//========================================================================
//  SE5 Design Creation - Captain Kwok's Balance Mod v125+
//========================================================================
//
//  Create AI Designs:
//  1. Is it time to create new designs?
//     a. Yes - New technology found
//     b. Yes - No designs at all
//     c. Yes - A design strategy change has occurred (combat ships only)
//     d. Yes - Our current design is very old (combat ships only)
//
//  2. For each design type
//     a. Get latest comps of each ability
//     b. Get possible components for this design
//     c. Get possible sizes for this design
//     d. Get weapon picks for this design
//     e. Is this design type needed?
//        1.  Yes - No current design of this type
//        2.  Yes - Current design version could be upgraded
//        3.  Yes - Majority Weapon has better selection
//        4.  Yes - Secondary Weapon has better selection
//        5.  Yes - Is there a larger size available than that used on Current version
//        6.  Yes - Newer engines available
//        7.  Yes - Newer shields available
//        8.  Yes - Quantum Reactor now available
//        9.  No  - The current version was created recently
//        10. No  - Must Have Components are not available
//     f. Create Design (see below)
//        1.  Add to the Vehicle Designs list
//        2.  Make old designs of this same type obsolete
//
//  Create Design:
//  (when adding comps, pick the best comp enhancement for each)
//  1. Set Owner
//  2. Set AI Design Type
//  3. Set Design Class Name
//  4. Set Date Created
//  5. Set Obsolete = FALSE
//  6. Set Design Type
//  7. Get New Design Name
//  8. Pick a vehicle size
//  9. Add required components:
//     a. Add Bridge
//     b. Add Life Support
//     c. Add Crew Quarters
//     d. Add Engines
//     e. Add Fighter Bays (Carriers)
//     f. Add Colony Module (Colonizers)
//     g. Add Cargo Bays/Supply/Ordnance/Remote Mining/Fighter Bays etc.
// 10. Add other components
//     a. Add Shields
//     b. Add Armor
//     c. Add Weapons
//     d. Add extra components (sensors, etc)
// 11. Set Strategy
//
//  Balance Mod Changes:
//  --------------------
//  v1.29 Changed - Bulkheads will be added to outer hull locations only
//        Changed - Added condition to spread components randomly versus just from top or just from bottom
//        Fixed   - Sometimes ships could add a bunch of Point-Defense due to a variable not being reset (v1.29b)
//        Changed - AI players won't use mounts with Allegiance Subverters
//  v1.27 Changed - Crystalline races will use specific numbers of shield generators (not % based like other races)
//  v1.25 Changed - Updated for changes to vehicle sizes and design types
//        Changed - Added support for the many new components introduced in v125
//        Changed - Added support for new design types introduced in v125
//        Changed - Added dynamic carrier design (high launch vs high storage)
//        Changed - Crystalline racial trait designs won't use shield regenerators
//        Changed - Religious racial trait designs will use fewer weapons on large designs to accommodate specialty components
//  v1.23 Added   - Support for AI players to use Psychic Node or Neural Combat Interface
//  v1.22 Fixed   - Basic sensors were not being added to the design type Satellite (Recon)
//  v1.20 Changed - Updated design type name references
//        Changed - Updated engine, crew quarters and life support settings
//        Changed - Non-combat ships will use Control Computers instead of Master Computers
//        Changed - Updated shield generation selection for new shield types
//  v1.19 Changed - Updated scripts to place Energy Refractors on outer hull slots
//        Fixed   - Sometimes the AI was not adding enough shields to their ship designs
//        Changed - Adjusted Drone Launcher/Fighter Bay requirements for Carriers (v1.19a)
//        Changed - Reduced armor usage on non-combat ships (v1.19a)
//        Fixed   - AI was not setting the correct strategy for Kamikaze Attack Ships (v1.19a)
//        Changed - Made design improvements to AI scout/explorer ship designs (v1.19a)
//        Changed - Updated Emissive Armor requirements for Drones/Weapon Platforms (v1.19b)
//        Fixed   - Sometimes a ship would have mismatched AI/Player design types (v1.19d)
//        Changed - Made some improvements to the Defense Base design (v1.19e)
//        Changed - The AI will only add a maximum of 1 Energy Refractor to a design (v1.19f)
//        Fixed   - AI players were not adding Supply/Ordnance components to their carrier designs (v1.19f)
//        Changed - Made improvements to protect AI Colony Ships against boarding actions (v1.19f)
//        Changed - Temporarily restricted the AI from adding Cloaking Devices to their Ships (v1.19f)
//        Changed - Increased Point-Defense usage on some AI designs (v1.19g)
//        Changed - Increased Shield Regenerator usage on some AI designs (v1.19g)
//        Fixed   - Recon Satellites will always add at least Basic Sensors (v1.19g)
//        Fixed   - It was possible for AI players to add two Stealth Totems on a design (v1.19g)
//        Fixed   - Some AI unit designs failed due to extra Emissive Armor components (v1.19g)
//        Added   - AI Empires will add letter suffixes to the names of upgraded design types (v1.19i)
//        Changed - Adjustments to Mine Layer and Satellite Layer design types (v1.19i)
//        Changed - Adjustments to Explorer and Scout Ship design types (v1.19j)
//        Changed - AI will sometimes add Long Range Scanners to a design (v.19j)
//        Fixed   - AI players would still sometimes add more than one Energy Refractor (v1.19j)
//        Fixed   - AI won't create designs without weapons when they should have them (v1.19j)
//  v1.18 Changed - Smaller ships will put their life support and crew quarters in internal slots only, larger ships will place more in inner slots
//        Changed - Improved Base Space Yard design to increase its defensive value
//  v1.17 Fixed   - Sometimes an AI would add a Shield Regenerator when no Shield Generator was present
//        Changed - Crystalline designs would sometimes lack a shield generator
//  v1.15 Changed - Updated component name references
//        Fixed   - When players were using ministers and copied a design, it would have the wrong AI design type
//        Added   - Fx[Get_Num_Weapon_To_Add] to help the AI add an appropriate amount of weapons
//        Fixed   - The problem with mismatched AI design types and player design types still persisted
//        Changed - Some AI design types will add less engines to fit specialty components
//  v1.14 Added   - Support for additional AI weapon choices
//        Added   - Support for AI ship size distribution scheme
//        Fixed   - AI was not always dispersing Life Support and Crew Quarters between inner and outer slots
//  v1.12 Fixed   - Error in designs for Recon Satellite and Medical Ship
//        Fixed   - All player design types will now have a matching AI design type
//        Changed - Added primary weapon for Troop Transport design
//        Fixed   - Improved extra component addition for troop and fighter designs
//  v1.11 Fixed   - The AI wasn't assigning the correct strategies for some designs
//  v1.10 Changed - AI will use carrier hulls for Drone Carrier
//        Changed - Adjusted AI designs to use 5kT armor
//        Added   - Support for new AI Design types
//        Changed - Reduced the number of engines that the AI adds for fighters
//  v1.07 Added   - New Fx[Get_Component_Percentage_To_Add] for adding required components to a design
//        Added   - Moved Fx[Get_Extra_Components_To_Add] from Script_AI_GlobalSettings.txt
//        Added   - Support for new AI design types
//        Changed - Updated almost all AI designs to improve use of additional components
//  v1.06 Added   - Design support for Medical Ships
//        Fixed   - Medium and Large Freighters were not being properly designed when Master Computers became available
//        Added   - New Fx[Set_Design_Type_Strategy] to set design strategies
//  v1.05 Fixed   - Carriers were not adding Ordnance Storage
//  v1.04 Changed - Number of crew/engines set based on AI vehicle size, rather than direct variables
//        Added   - Support for Patrol and Scout Ships
//        Added   - Support for multiple Drone design types
//        Added   - Boolean variables to control design of Stellar Manipulation ships
//  v1.03 Changed - Only combat designs that use ordnance will add Ordnance Storage
//  v1.02 Changed - AI will now consider the level of Crew Quarter and Life Support components when adding them to a design
//        Fixed   - AI designs will use up last 5kT of space
//  v1.00 Added   - Support for Small Organic/Crystalline Armor on Fighters/Troops
//        Fixed   - Satellites were not adding Basic Sensors
//  v0.99 Fixed   - AI wasn't adding to max engines on larger ships
//        Added   - Bombardment Ship design type
//  v0.98 Fixed   - Design type reference for Weapon Platforms
//        Changed - AI will use Master Computer if it is available
//  v0.97 Added   - Modifier for tonnage percent
//        Added   - Non-racial trait empires will use an armor mixture
//        Added   - New Attack and Defense designs for Drones
//        Added   - Point-Defense weapons choices now pick in Fx[Get_Best_Component_Of_Type] not from list in Fx[AI_Add_Extra_Comps]
//  v0.96 Added   - Defense Ship design type
//        Added   - Support for Secondary weapon choices
//  v0.95 Changed - Set AI Carriers to only use 30% of the hull to Fighter Bays
//        Added   - Heavy weapon type for Weapon Platforms
//  v0.92 Changed - Tweaked Point-Defense Ship design
//  v0.91 Added   - Point-Defense Ship design type
//  v0.84 Added   - Support for a couple more design types
//  v0.82 Added   - Support for updated design types
//  v0.73 Changed - Modified Cargo Transport to carry Supply/Ordnance Storage
//        Changed - Made changes to allow better design specification of units
//
//  Balance Mod To-do list:
//  -----------------------
//  - Continue to improve AI designs (v125+)
//    - Improve shield/armor balance
//
//  Script Function Requests:
//  -------------------------
//  - None pending

//------------------------------------------------------------------------
// Global Variables
//------------------------------------------------------------------------
globalvars

  gbl_design_name:           string
  gbl_design_size:           long
  gbl_design_count:          long

endglobalvars

//------------------------------------------------------------------------
// Forward Declarations
//------------------------------------------------------------------------
deffunc

function Set_AI_Design_Type_For_Design returns boolean
params
  design_id:                 long
  plr_design_type:           string
  ai_design_type:            string
end

function Create_Design returns boolean
params
  ai_design_type:            string
  ai_design_type_index:      long
end

function Add_Components_To_Design returns boolean
params
  design_id:                 long
  ai_design_type:            string
end

function Is_AI_Design_Type_Needed returns boolean
params
  ai_design_type:            string
  ai_design_size:            long
end

function Add_Components_To_Vehicle_Design returns boolean
params
  design_id:                 long
  comp_id:                   long
  comp_enh_id:               long
  ship_section_id:           long
  num_comps_to_add:          long
  spread_direction:          long
end

function Get_Best_Component_Of_Type returns long
params
  comp_type:                 string
end

function Pick_Vehicle_Size returns long
params
  ai_design_type:            string
  ai_design_type_index:      long
end

function Get_Weapon_Tonnage_To_Add returns boolean
params
  vehicle_type:              string
  vehicle_tonnage:           long
  ai_design_type:            string
end

function Get_Component_Percentage_To_Add returns boolean
params
  ai_design_type:            string
  vehicle_tonnage:           long
end

function Get_Extra_Components_To_Add returns boolean
params
  vehicle_type:              string
  vehicle_tonnage:           long
end

function Get_Number_Of_Components_To_Add returns long
params
  design_id:                 long
  design_type:               string
  comp_type:                 string
end

function Add_Required_Components_To_Design returns boolean
params
  design_id:                 long
  ai_design_type:            string
end

function Mark_Old_Designs_Of_Type_Obsolete returns boolean
params
  design_id:                 long
  ai_design_type:            string
end

function Get_Wanted_Component_From_List returns long
params
  lst_wanted_names:          stringlist
end

function Get_AI_Vehicle_Size returns long
params
  size_name:                 string
end

function Set_Design_Type_Strategy returns boolean
params
  design_id:                 long
  design_type:               string
end

enddeffunc

//------------------------------------------------------------------------
// AI_DesignCreation
//------------------------------------------------------------------------
function AI_DesignCreation returns boolean
params
vars
  plr_design_type:           string
  ai_design_type:            string
  ai_design_size:            long
  ai_design_index:           long
  design_type_count:         long
  index:                     long
  design_count:              long
  design_list:               longlist
  design_id:                 long
  lst_design_type_to_create: stringlist
  lst_design_type_index:     longlist
  lst_design_type_name:      stringlist
  lst_design_type_size:      longlist
begin

  call Set_Design_Suffixes()

  // Go through our designs and find any with a mismatched AI and general design type
  set design_count := Sys_Get_Player_Designs_Count(sys_long_Player_ID)

  if (design_count > 0) then
    for index := 1 to design_count do
      set design_id := Sys_Get_Player_Design_ID(sys_long_Player_ID, index)
      set plr_design_type := Sys_Get_Vehicle_Design_General_Type(sys_long_Player_ID, design_id)
      set ai_design_type := Sys_Get_Vehicle_Design_AI_Design_Type(design_id)
      // Does the AI design type match the general design type?
      if (plr_design_type <> ai_design_type) then
        call Set_AI_Design_Type_For_Design(design_id, plr_design_type, ai_design_type)
      endif
    endfor
  endif

  // Compile a list of our wanted design types
  if Sys_Using_AI_Minister(sys_long_Player_ID, "Vehicle Design") then
    set design_type_count := lst_AI_Design_Type_Name.count()
    // Debug output
    call Sys_Debug_Print("Design", "----------------")
    call Sys_Debug_Print("Design", "Design Creation:")
    // Go through each valid design type to decide if we want to make a design for it
    if (design_type_count > 0) then
      for index := 1 to design_type_count do
        set ai_design_type := lst_AI_Design_Type_Name.get(index)
        set ai_design_size := lst_AI_Design_Type_Vehicle_Size.get(index)
        // Do we need this design type?
        if Is_AI_Design_Type_Needed(ai_design_type, ai_design_size) then
          call lst_design_type_to_create.add(ai_design_type)
          call lst_design_type_index.add(index)
          call lst_design_type_name.add(gbl_design_name)
          call lst_design_type_size.add(gbl_design_size)
        endif
      endfor
    endif

    // Create the designs for our wanted design types
    set design_type_count := lst_design_type_to_create.count()
    if (design_type_count > 0) then
      call Sys_Prepare_For_Vehicle_Design_Creation(sys_long_Player_ID)
      for index := 1 to design_type_count do
        set ai_design_type := lst_design_type_to_create.get(index)
        set ai_design_index := lst_design_type_index.get(index)
        set gbl_design_name := lst_design_type_name.get(index)
        set gbl_design_size := lst_design_type_size.get(index)
        call Create_Design(ai_design_type, ai_design_index)
      endfor
    endif
  endif

  // Reset our Design upgrade modifier
  if (gbl_design_count > 0) then
    set lng_AI_Design_Upgrade_Modifier := 0
    // Save the updated design upgrade modifier
    call Sys_Set_AI_Storage_Long(sys_long_Player_ID, 1, 8, lng_AI_Design_Upgrade_Modifier)
  endif

  return TRUE
end

//------------------------------------------------------------------------
// Create_Specific_Design (Auto-Complete Function)
//------------------------------------------------------------------------
function Create_Specific_Design returns boolean
params
  design_id:                 long
vars
  new_design_name:           string
  bool_continue_design:      boolean
  plr_design_type:           string
  ai_design_type:            string
begin
  return FALSE

  // Prepare for design creation
  call Sys_Prepare_For_Vehicle_Design_Creation(sys_long_Player_ID)

  // Get the player's design type
  set plr_design_type := Sys_Get_Vehicle_Design_General_Type(sys_long_Player_ID, design_id)
  
  // Convert the player's design type to an AI design type
  call Set_AI_Design_Type_For_Design(design_id, plr_design_type, "")
  set ai_design_type := Sys_Get_Vehicle_Design_AI_Design_Type(design_id)

  // Find a design name
  set new_design_name := Sys_Get_New_Vehicle_Design_Name_Not_In_Use(sys_long_Player_ID)
  call Sys_Set_Vehicle_Design_Name(design_id, new_design_name)

  // Add required components to this design
  set bool_continue_design := Add_Required_Components_To_Design(design_id, ai_design_type)

  // Add the remainder of components to this design
  if (bool_continue_design) then
    set bool_continue_design := Add_Components_To_Design(design_id, ai_design_type)
  endif

end

//------------------------------------------------------------------------
// Set_AI_Design_Type_For_Design
//------------------------------------------------------------------------
function Set_AI_Design_Type_For_Design returns boolean
params
  design_id:                 long
  plr_design_type:           string
  ai_design_type:            string
vars
begin

  // Update the AI design type if the player design type is different
  if (ai_design_type = "") or (ai_design_type <> plr_design_type) then
    call Sys_Set_Vehicle_Design_AI_Design_Type(sys_long_Player_ID, design_id, plr_design_type)
  endif

  return TRUE
end

//------------------------------------------------------------------------
// Is_AI_Design_Type_Needed
//------------------------------------------------------------------------
function Is_AI_Design_Type_Needed returns boolean
params
  ai_design_type:            string
  ai_design_size:            long
vars
  design_needed:             boolean := FALSE
  update_design:             boolean := FALSE
  combat_design:             boolean := FALSE
  prev_design_id:            long
  prev_design_date:          long
  prev_design_age:           long
  prev_design_name:          string
  prev_design_name_length:   long
  design_suffix_pos:         long
  design_suffix:             string
  new_design_suffix:         string
  suffix_index:              long
begin

  // Get our current design id
  set prev_design_id := Sys_Get_Latest_Vehicle_Design_Of_AI_Type(sys_long_Player_ID, ai_design_type)
  set prev_design_date := Sys_Get_Vehicle_Design_Creation_Date(sys_long_Player_ID, prev_design_id)
  set prev_design_age := sys_long_Game_Date - prev_design_date

  // Get our design upgrade modifier
  set lng_AI_Design_Upgrade_Modifier := Sys_Get_AI_Storage_Long(sys_long_Player_ID, 1, 8)

  // Reset global design name
  set gbl_design_name := ""
  set gbl_design_size := 0

  // Debug output
  call Sys_Debug_Print("Design", "  - Reviewing " + ai_design_type + " design")

  // Note our combat designs
  if (Sys_Pos_String("Attack", ai_design_type) > 0) or (Sys_Pos_String("Defense", ai_design_type) > 0) or (Sys_Pos_String("Seeker", ai_design_type) > 0) or (Sys_Pos_String("Patrol", ai_design_type) > 0) or (Sys_Pos_String("Point-Defense", ai_design_type) > 0) then
    set combat_design := TRUE
    call Sys_Debug_Print("Design", "    - Combat design")
  endif

  // Do we have any designs of this type?
  if (prev_design_id = 0) then
    set design_needed := TRUE
    call Sys_Debug_Print("Design", "    - New design required")
  else
    call Sys_Debug_Print("Design", "    - Design age = " + Sys_Convert_Long_To_String(prev_design_age))
  endif

  // Have we achieved important new technology or is the design simply getting old?
  if (prev_design_id > 0) then
    if (lng_AI_Design_Upgrade_Modifier >= 3) or (prev_design_age > 25) then
      set update_design := TRUE
    endif
  endif

  // For our current design of this type, can it be upgraded?
  if (update_design) then
    if Sys_Can_Vehicle_Design_Be_Upgraded(sys_long_Player_ID, prev_design_id) then
      set design_needed := TRUE
      set prev_design_name := Sys_Get_Vehicle_Design_Name(prev_design_id)
      set prev_design_name := Sys_Right_Trim_String(prev_design_name)
      set gbl_design_size := Sys_Get_Vehicle_Design_Size_ID(prev_design_id)
      // Identify current design name suffix
      set prev_design_name_length := Sys_Length_String(prev_design_name)
      // Identify the suffix
      set design_suffix_pos := Get_Design_Suffix_Position(prev_design_name)
      set design_suffix := Sys_Copy_String(prev_design_name, design_suffix_pos, 7)
      set suffix_index := lst_AI_Design_Suffixes.indexof(design_suffix)
      // Identify which suffix we should use for our design
      if (suffix_index < 30) then
        set new_design_suffix := lst_AI_Design_Suffixes.get(suffix_index + 1)
        // Add a suffix if there isn't one
        if (suffix_index <= 0) then
          set gbl_design_name := prev_design_name + new_design_suffix
        else
          // Update the current suffix
          set prev_design_name := Sys_Delete_String(prev_design_name, design_suffix_pos, 7)
          set gbl_design_name := prev_design_name + new_design_suffix
        endif
      endif
      // Debug output
      call Sys_Debug_Print("Design", "    - Design can be upgraded")
    else
      if (bool_Race_Prompt_Design_Strategy_Change) and (combat_design) then
        set design_needed := TRUE
        // Debug output
        call Sys_Debug_Print("Design", "    - Forcing design change due to design strategy change")
      else
        if (prev_design_age > 100) and (combat_design) then
          // Debug output
          set design_needed := TRUE
          call Sys_Debug_Print("Design", "    - Forcing design change due to extreme design age")
        else
          // Debug output
          call Sys_Debug_Print("Design", "    - Design can't be upgraded")
        endif
      endif
    endif
  endif

  return design_needed
end

//------------------------------------------------------------------------
// Create_Design
//------------------------------------------------------------------------
function Create_Design returns boolean
params
  ai_design_type:            string
  ai_design_type_index:      long
vars
  design_id:                 long
  design_cost:               resources
  choose_size:               long
  new_design_name:           string
  bool_name_ok:              boolean
  bool_continue_design:      boolean
  bool_design_created:       boolean
begin
  return FALSE

  // Debug output
  call Sys_Debug_Print("Design", "  - Attempt to design " + ai_design_type)

  //  1. Set Owner
  set design_id := Sys_Create_Temporary_Vehicle_Design(sys_long_Player_ID)

  //  2. Set AI Design Type
  call Sys_Set_Vehicle_Design_Ai_Design_Type(sys_long_Player_ID, design_id, ai_design_type)

  //  3. Set Design Class Name
  call Sys_Set_Vehicle_Design_Class_Name(design_id, ai_design_type)

  //  5. Set Obsolete = FALSE
  call Sys_Set_Vehicle_Design_Obsolete(design_id, FALSE)

  //  6. Set Design Type
  call Sys_Set_Vehicle_Design_General_Type(design_id, ai_design_type)

  //  8. Pick a vehicle size
  set choose_size := Pick_Vehicle_Size(ai_design_type, ai_design_type_index)

  //  7. Get New Design Name
  if (gbl_design_name <> "") and (not Sys_Is_Vehicle_Design_Name_In_Use(gbl_design_name)) and (choose_size = gbl_design_size) then
    set new_design_name := gbl_design_name
  else      
    set new_design_name := Get_Unique_Vehicle_Design_Name()
  endif

  // Remove trailing spaces and set the name for our design
  call Sys_Right_Trim_String(new_design_name)
  call Sys_Set_Vehicle_Design_Name(design_id, new_design_name)

  //  9. Add Requirement components to the design
  if (choose_size > 0) then
    call Sys_Set_Vehicle_Design_Size_ID(sys_long_Player_ID, design_id, choose_size)
    set bool_continue_design := Add_Required_Components_To_Design(design_id, ai_design_type)

    // 10. Add other components to the design
    if (bool_continue_design) then
      set bool_continue_design := Add_Components_To_Design(design_id, ai_design_type)
    endif

    // 11. Set Design Type Strategy
    if (bool_continue_design) then
      set bool_continue_design := Set_Design_Type_Strategy(design_id, ai_design_type)
    endif

    if (bool_continue_design) then
      set design_id := Sys_Create_Permanent_Vehicle_Design(sys_long_Player_ID, design_id)
      if (design_id > 0) then
        call Mark_Old_Designs_Of_Type_Obsolete(design_id, ai_design_type)
        return TRUE
        set gbl_design_count := gbl_design_count + 1
        // Debug output
        call Sys_Debug_Print("Design", "    - Design " + new_design_name + " created")
      endif
    else
      // Debug output
      call Sys_Debug_Print("Design", "    - Design " + new_design_name + " not created")
    endif
  endif

end

//------------------------------------------------------------------------
// Get_Component_Percentage_To_Add
//------------------------------------------------------------------------
function Get_Component_Percentage_To_Add returns boolean
params
  ai_design_type:            string
  vehicle_tonnage:           long
vars
  armor_mod_pct:             long
  shield_mod_pct:            long
  pd_mod_amt:                long
  have_shields:              boolean := FALSE
  have_phased_shields:       boolean := FALSE
begin

  // Shields and Armor
  set lng_AI_Design_Pct_Shields                            := 0
  set lng_AI_Design_Pct_Primary_Armor                      := 0
  set lng_AI_Design_Pct_Secondary_Armor                    := 0

  // Other Components
  set lng_AI_Design_Pct_Repair_Bay                         := 0
  set lng_AI_Design_Pct_Cargo_Bay                          := 0
  set lng_AI_Design_Pct_Supply_Storage                     := 0
  set lng_AI_Design_Pct_Ordnance_Storage                   := 0
  set lng_AI_Design_Pct_Solar_Collector                    := 0
  set lng_AI_Design_Pct_Supply_Generation                  := 0

  // Weapons
  set bool_AI_Design_Add_Primary_Weapon                    := FALSE
  set bool_AI_Design_Add_Secondary_Weapon                  := FALSE
  set bool_AI_Design_Add_Heavy_Weapon                      := FALSE
  set bool_AI_Design_Add_Special_Weapon                    := FALSE
  set bool_AI_Design_Add_Primary_PD                        := FALSE
  set bool_AI_Design_Add_Secondary_PD                      := FALSE
  set bool_AI_Design_Add_Special_PD                        := FALSE

  set lng_AI_Design_Max_Num_Primary_Weapon                 := 99
  set lng_AI_Design_Max_Num_Secondary_Weapon               := 99
  set lng_AI_Design_Max_Num_Heavy_Weapon                   := 99
  set lng_AI_Design_Max_Num_Special_Weapon                 := 99
  set lng_AI_Design_Max_Num_Primary_PD                     := 99
  set lng_AI_Design_Max_Num_Secondary_PD                   := 99
  set lng_AI_Design_Max_Num_Special_PD                     := 99

  // Extra components
  set bool_AI_Design_Add_Bulkheads                         := FALSE
  set bool_AI_Design_Add_Stealth_Armor                     := FALSE
  set bool_AI_Design_Add_Scattering_Armor                  := FALSE
  set bool_AI_Design_Add_Energy_Refractor                  := FALSE
  set bool_AI_Design_Add_Shields                           := FALSE
  set bool_AI_Design_Add_Supply_Storage                    := FALSE
  set bool_AI_Design_Add_Ordnance_Storage                  := FALSE
  set bool_AI_Design_Skip_Ordnance_Storage                 := FALSE
  set bool_AI_Design_Add_Deflector_Array                   := FALSE
  set bool_AI_Design_Add_Combat_Sensors                    := FALSE
  set bool_AI_Design_Add_Sensors                           := FALSE
  set bool_AI_Design_Skip_Basic_Sensors                    := FALSE
  set bool_AI_Design_Add_Long_Range_Scanners               := FALSE
  set bool_AI_Design_Add_Scanner_Jammer                    := FALSE
  set bool_AI_Design_Add_ECM                               := FALSE
  set bool_AI_Design_Add_Supply_Generation                 := FALSE
  set bool_AI_Design_Add_Shield_Regenerator                := FALSE
  set bool_AI_Design_Add_Cloaking_Device                   := FALSE
  set bool_AI_Design_Add_Extra_Movement                    := FALSE
  set bool_AI_Design_Add_Combat_Movement                   := FALSE
  set bool_AI_Design_Add_Solar_Collector                   := FALSE
  set bool_AI_Design_Add_Security_Station                  := FALSE
  set bool_AI_Design_Add_Self_Destruct                     := FALSE
  set bool_AI_Design_Add_Repair_Bay                        := FALSE
  set bool_AI_Design_Add_Medical_Bay                       := FALSE
  set bool_AI_Design_Add_Talisman                          := FALSE
  set bool_AI_Design_Add_Holy_War_Totem                    := FALSE
  set bool_AI_Design_Add_Vengeance_Totem                   := FALSE
  set bool_AI_Design_Add_Stealth_Totem                     := FALSE
  set bool_AI_Design_Add_Combat_Best_Experience            := FALSE
  set bool_AI_Design_Add_Turbocharger                      := FALSE
  set bool_AI_Design_Add_Afterburners                      := FALSE
  set bool_AI_Design_Add_Combat_Thruster                   := FALSE
  set bool_AI_Design_Add_No_Weapons                        := FALSE

  // Note our shield usage
  set have_shields := Is_Component_Available("Shield Generator")
  set have_phased_shields := Is_Component_Available("Phased Shield Generator")

  // Adjust various percentages based on ship type, design type, racial traits and other factors
  // Crystalline races should use armor more
  if (bool_Race_Uses_Crystalline_Tech) then
    if (vehicle_tonnage > 500) then
      set armor_mod_pct := armor_mod_pct + 5
      set shield_mod_pct := shield_mod_pct - 5
    endif
  endif
  // Organic races should use armor more
  if (bool_Race_Uses_Organic_Tech) then
    set armor_mod_pct := armor_mod_pct + 5
    set shield_mod_pct := shield_mod_pct - 5
  endif

  // Religious races need to keep extra space open for specialty components
  if (bool_Race_Uses_Religious_Tech) then
    set armor_mod_pct := armor_mod_pct - 3
  endif

  // Increase armor usage if we don't have shields
  if (not have_shields) then
    set armor_mod_pct := armor_mod_pct + 5
    set shield_mod_pct := shield_mod_pct - 5
  else
    // If our enemies are using skip armor weapons or boarding parties, increase our shield usage
    if (bool_Enemy_Uses_Skip_Armor_Weapons) or (bool_Enemy_Uses_Boarding_Parties) then
      set armor_mod_pct := armor_mod_pct - 5
      set shield_mod_pct := shield_mod_pct + 5
    endif
  endif

  // Increase armor usage if our enemies use skip shield weapons or shield depleting weapons
  if ((bool_Enemy_Uses_Skip_Shield_Weapons) and (not have_phased_shields)) or (bool_Enemy_Uses_Shield_Depleting_Weapons) then
    set armor_mod_pct := armor_mod_pct + 5
    set shield_mod_pct := shield_mod_pct - 5
  endif

  // Increase our Point Defense usage
  if (bool_Enemy_Uses_Seeker_Weapons) then
    set pd_mod_amt := pd_mod_amt + 1
  endif
  if (bool_Enemy_Uses_Long_Range_Seeker_Weapons) then
    set pd_mod_amt := pd_mod_amt + 1
  endif

  // Convert our design's AI design type to a general AI design type
  set ai_design_type := Get_General_AI_Design_Type(ai_design_type)

  // Determine components to add based on our design type
  case ai_design_type
    "Ship (Attack)":
      set lng_AI_Design_Pct_Primary_Armor                  := 2 + armor_mod_pct
      set lng_AI_Design_Pct_Secondary_Armor                := 5
      set lng_AI_Design_Pct_Shields                        := 10 + shield_mod_pct

      set bool_AI_Design_Add_Bulkheads                     := bool_Enemy_Uses_Skip_Armor_Weapons or bool_Enemy_Uses_Skip_All_Weapons
      set bool_AI_Design_Add_Stealth_Armor                 := (vehicle_tonnage > 500)
      set bool_AI_Design_Add_Scattering_Armor              := (vehicle_tonnage > 500)
      set bool_AI_Design_Add_Energy_Refractor              := (vehicle_tonnage > 500) and (bool_Enemy_Uses_Direct_Fire_Weapons)
      set bool_AI_Design_Add_Primary_Weapon                := TRUE
      set bool_AI_Design_Add_Secondary_Weapon              := FALSE
      set bool_AI_Design_Add_Heavy_Weapon                  := (vehicle_tonnage >= 400)
      set bool_AI_Design_Add_Special_Weapon                := (vehicle_tonnage >= 750)
      set bool_AI_Design_Add_Primary_PD                    := TRUE
      set bool_AI_Design_Add_Secondary_PD                  := (vehicle_tonnage > 600)
      set bool_AI_Design_Add_Special_PD                    := (vehicle_tonnage > 900)
      set bool_AI_Design_Add_Special_Armor                 := (vehicle_tonnage > 750)
      set bool_AI_Design_Add_Supply_Storage                := bool_Enemy_Uses_Only_Engine_Weapons
      set bool_AI_Design_Add_Deflector_Array               := (vehicle_tonnage > 600)
      set bool_AI_Design_Add_Combat_Sensors                := TRUE
      set bool_AI_Design_Add_ECM                           := TRUE
      set bool_AI_Design_Add_Supply_Generation             := (vehicle_tonnage > 500)
      set bool_AI_Design_Add_Shield_Regenerator            := (vehicle_tonnage > 500) and (lng_AI_Design_Pct_Shields * vehicle_tonnage > 3000)
      set bool_AI_Design_Add_Extra_Movement                := (vehicle_tonnage > 600)
      set bool_AI_Design_Add_Combat_Movement               := (vehicle_tonnage > 600)
      set bool_AI_Design_Add_Cloaking_Device               := bool_Race_Uses_Cloaking_Devices and (vehicle_tonnage > 600)
      set bool_AI_Design_Add_Security_Station              := bool_Enemy_Uses_Boarding_Parties and (vehicle_tonnage > 700)
      set bool_AI_Design_Add_Self_Destruct                 := bool_Enemy_Uses_Boarding_Parties and (vehicle_tonnage > 750)
      set bool_AI_Design_Add_Stealth_Totem                 := bool_Race_Uses_Religious_Tech and (vehicle_tonnage > 750)
      set bool_AI_Design_Add_Talisman                      := bool_Race_Uses_Religious_Tech and (vehicle_tonnage > 750)
      set bool_AI_Design_Add_Vengeance_Totem               := bool_Race_Uses_Religious_Tech and (vehicle_tonnage > 900)
      set bool_AI_Design_Add_Holy_War_Totem                := bool_Race_Uses_Religious_Tech and (vehicle_tonnage > 1200)
      set bool_AI_Design_Add_Combat_Best_Experience        := bool_Race_Uses_Combat_Best_Experience and (vehicle_tonnage > 600)

      set lng_AI_Design_Max_Num_Special_Weapon             := Sys_Divide_Long(vehicle_tonnage, 600)

      if (bool_Enemy_Uses_Seeker_Weapons) or (bool_Enemy_Uses_Long_Range_Seeker_Weapons) then
        set lng_AI_Design_Max_Num_Primary_PD               := 1 + Sys_Divide_Long(vehicle_tonnage, 400)
        set lng_AI_Design_Max_Num_Secondary_PD             := Sys_Divide_Long(vehicle_tonnage, 600)
        set lng_AI_Design_Max_Num_Special_PD               := Sys_Divide_Long(vehicle_tonnage, 1200)
      else
        set lng_AI_Design_Max_Num_Primary_PD               := 1 + Sys_Divide_Long(vehicle_tonnage, 750)
        set lng_AI_Design_Max_Num_Secondary_PD             := Sys_Divide_Long(vehicle_tonnage, 800)
        set lng_AI_Design_Max_Num_Special_PD               := 1
      endif

    "Ship (Defense)":
      set lng_AI_Design_Pct_Primary_Armor                  := 2 + armor_mod_pct
      set lng_AI_Design_Pct_Secondary_Armor                := 5
      set lng_AI_Design_Pct_Shields                        := 10 + shield_mod_pct

      set bool_AI_Design_Add_Bulkheads                     := bool_Enemy_Uses_Skip_Armor_Weapons or bool_Enemy_Uses_Skip_All_Weapons
      set bool_AI_Design_Add_Stealth_Armor                 := (vehicle_tonnage > 500)
      set bool_AI_Design_Add_Scattering_Armor              := (vehicle_tonnage > 500)
      set bool_AI_Design_Add_Energy_Refractor              := (vehicle_tonnage > 500) and (bool_Enemy_Uses_Direct_Fire_Weapons)
      set bool_AI_Design_Add_Primary_Weapon                := FALSE
      set bool_AI_Design_Add_Secondary_Weapon              := TRUE
      set bool_AI_Design_Add_Heavy_Weapon                  := (vehicle_tonnage >= 400)
      set bool_AI_Design_Add_Special_Weapon                := (vehicle_tonnage >= 750)
      set bool_AI_Design_Add_Primary_PD                    := TRUE
      set bool_AI_Design_Add_Secondary_PD                  := (vehicle_tonnage > 600)
      set bool_AI_Design_Add_Special_PD                    := (vehicle_tonnage > 900)
      set bool_AI_Design_Add_Special_Armor                 := (vehicle_tonnage > 750)
      set bool_AI_Design_Add_Supply_Storage                := bool_Enemy_Uses_Only_Engine_Weapons
      set bool_AI_Design_Add_Deflector_Array               := (vehicle_tonnage > 600)
      set bool_AI_Design_Add_Combat_Sensors                := TRUE
      set bool_AI_Design_Add_ECM                           := TRUE
      set bool_AI_Design_Add_Supply_Generation             := (vehicle_tonnage > 500)
      set bool_AI_Design_Add_Shield_Regenerator            := (vehicle_tonnage > 500) and (lng_AI_Design_Pct_Shields * vehicle_tonnage > 3000)
      set bool_AI_Design_Add_Extra_Movement                := (vehicle_tonnage > 600)
      set bool_AI_Design_Add_Combat_Movement               := (vehicle_tonnage > 600)
      set bool_AI_Design_Add_Cloaking_Device               := bool_Race_Uses_Cloaking_Devices and (vehicle_tonnage > 600)
      set bool_AI_Design_Add_Security_Station              := bool_Enemy_Uses_Boarding_Parties and (vehicle_tonnage > 700)
      set bool_AI_Design_Add_Self_Destruct                 := bool_Enemy_Uses_Boarding_Parties and (vehicle_tonnage > 750)
      set bool_AI_Design_Add_Stealth_Totem                 := bool_Race_Uses_Religious_Tech and (vehicle_tonnage > 750)
      set bool_AI_Design_Add_Talisman                      := bool_Race_Uses_Religious_Tech and (vehicle_tonnage > 750)
      set bool_AI_Design_Add_Vengeance_Totem               := bool_Race_Uses_Religious_Tech and (vehicle_tonnage > 900)
      set bool_AI_Design_Add_Holy_War_Totem                := bool_Race_Uses_Religious_Tech and (vehicle_tonnage > 1200)

      set lng_AI_Design_Max_Num_Special_Weapon             := Sys_Divide_Long(vehicle_tonnage, 500)

      if (bool_Enemy_Uses_Seeker_Weapons) or (bool_Enemy_Uses_Long_Range_Seeker_Weapons) then
        set lng_AI_Design_Max_Num_Primary_PD               := 1 + Sys_Divide_Long(vehicle_tonnage, 400)
        set lng_AI_Design_Max_Num_Secondary_PD             := Sys_Divide_Long(vehicle_tonnage, 600)
        set lng_AI_Design_Max_Num_Special_PD               := Sys_Divide_Long(vehicle_tonnage, 1200)
      else
        set lng_AI_Design_Max_Num_Primary_PD               := 1 + Sys_Divide_Long(vehicle_tonnage, 750)
        set lng_AI_Design_Max_Num_Secondary_PD             := Sys_Divide_Long(vehicle_tonnage, 1100)
        set lng_AI_Design_Max_Num_Special_PD               := 1
      endif

    "Ship (Seeker)":
      set lng_AI_Design_Pct_Primary_Armor                  := 2 + armor_mod_pct
      set lng_AI_Design_Pct_Secondary_Armor                := 5
      set lng_AI_Design_Pct_Shields                        := 10 + shield_mod_pct

      set bool_AI_Design_Add_Bulkheads                     := bool_Enemy_Uses_Skip_Armor_Weapons or bool_Enemy_Uses_Skip_All_Weapons
      set bool_AI_Design_Add_Stealth_Armor                 := (vehicle_tonnage > 500)
      set bool_AI_Design_Add_Scattering_Armor              := (vehicle_tonnage > 500)
      set bool_AI_Design_Add_Energy_Refractor              := (vehicle_tonnage > 500) and (bool_Enemy_Uses_Direct_Fire_Weapons)
      set bool_AI_Design_Add_Primary_Weapon                := (vehicle_tonnage >= 450)
      set bool_AI_Design_Add_Heavy_Weapon                  := TRUE
      set bool_AI_Design_Add_Special_Weapon                := (vehicle_tonnage > 600)
      set bool_AI_Design_Add_Primary_PD                    := (vehicle_tonnage > 500)
      set bool_AI_Design_Add_Secondary_PD                  := (vehicle_tonnage > 750)
      set bool_AI_Design_Add_Special_Armor                 := (vehicle_tonnage > 750)
      set bool_AI_Design_Add_Supply_Storage                := bool_Enemy_Uses_Only_Engine_Weapons
      set bool_AI_Design_Add_Deflector_Array               := (vehicle_tonnage > 600)
      set bool_AI_Design_Add_Combat_Sensors                := (vehicle_tonnage > 500)
      set bool_AI_Design_Add_ECM                           := TRUE
      set bool_AI_Design_Add_Supply_Generation             := (vehicle_tonnage > 500)
      set bool_AI_Design_Add_Shield_Regenerator            := (vehicle_tonnage > 500) and (lng_AI_Design_Pct_Shields * vehicle_tonnage > 3000)
      set bool_AI_Design_Add_Extra_Movement                := (vehicle_tonnage > 600)
      set bool_AI_Design_Add_Combat_Movement               := (vehicle_tonnage > 600)
      set bool_AI_Design_Add_Cloaking_Device               := bool_Race_Uses_Cloaking_Devices and (vehicle_tonnage > 600)
      set bool_AI_Design_Add_Security_Station              := bool_Enemy_Uses_Boarding_Parties and (vehicle_tonnage > 700)
      set bool_AI_Design_Add_Self_Destruct                 := bool_Enemy_Uses_Boarding_Parties and (vehicle_tonnage > 750)
      set bool_AI_Design_Add_Talisman                      := bool_Race_Uses_Religious_Tech and (vehicle_tonnage > 750)
      set bool_AI_Design_Add_Vengeance_Totem               := bool_Race_Uses_Religious_Tech and (vehicle_tonnage > 900)
      set bool_AI_Design_Add_Holy_War_Totem                := bool_Race_Uses_Religious_Tech and (vehicle_tonnage > 1200)

      set lng_AI_Design_Max_Num_Primary_Weapon             := Sys_Divide_Long(vehicle_tonnage, 500)
      set lng_AI_Design_Max_Num_Special_Weapon             := Sys_Divide_Long(vehicle_tonnage, 750)

      if (bool_Enemy_Uses_Seeker_Weapons) or (bool_Enemy_Uses_Long_Range_Seeker_Weapons) then
        set lng_AI_Design_Max_Num_Primary_PD               := 1 + Sys_Divide_Long(vehicle_tonnage, 400)
        set lng_AI_Design_Max_Num_Secondary_PD             := 1
        set lng_AI_Design_Max_Num_Special_PD               := 1
      else
        set lng_AI_Design_Max_Num_Primary_PD               := 1 + Sys_Divide_Long(vehicle_tonnage, 750)
        set lng_AI_Design_Max_Num_Secondary_PD             := 1
        set lng_AI_Design_Max_Num_Special_PD               := 1
      endif

    "Ship (Point-Defense)":
      set lng_AI_Design_Pct_Secondary_Armor                := 5 + armor_mod_pct
      set lng_AI_Design_Pct_Shields                        := 10 + shield_mod_pct

      set bool_AI_Design_Add_Bulkheads                     := bool_Enemy_Uses_Skip_Armor_Weapons or bool_Enemy_Uses_Skip_All_Weapons
      set bool_AI_Design_Add_Stealth_Armor                 := TRUE
      set bool_AI_Design_Add_Primary_Weapon                := TRUE
      set bool_AI_Design_Add_Primary_PD                    := TRUE
      set bool_AI_Design_Add_Secondary_PD                  := TRUE
      set bool_AI_Design_Add_Special_PD                    := TRUE
      set bool_AI_Design_Add_Supply_Storage                := bool_Enemy_Uses_Only_Engine_Weapons
      set bool_AI_Design_Add_Combat_Sensors                := TRUE
      set bool_AI_Design_Add_ECM                           := TRUE
      set bool_AI_Design_Add_Talisman                      := bool_Race_Uses_Religious_Tech and (vehicle_tonnage >= 500)

      set lng_AI_Design_Max_Num_Primary_Weapon             := 1 + Sys_Divide_Long(vehicle_tonnage, 500)
      set lng_AI_Design_Max_Num_Special_PD                 := 1 + Sys_Divide_Long(vehicle_tonnage, 600)

    "Ship (Boarding)":
      set lng_AI_Design_Pct_Primary_Armor                  := 2 + armor_mod_pct
      set lng_AI_Design_Pct_Secondary_Armor                := 4
      set lng_AI_Design_Pct_Shields                        := 10 + shield_mod_pct

      set bool_AI_Design_Add_Primary_Weapon                := TRUE
      set bool_AI_Design_Add_Special_Weapon                := TRUE
      set bool_AI_Design_Add_Primary_PD                    := (vehicle_tonnage >= 400)
      set bool_AI_Design_Add_Supply_Storage                := bool_Enemy_Uses_Only_Engine_Weapons
      set bool_AI_Design_Add_Combat_Sensors                := TRUE
      set bool_AI_Design_Add_ECM                           := TRUE

      set lng_AI_Design_Max_Num_Primary_Weapon             := 1
      set lng_AI_Design_Max_Num_Special_Weapon             := Sys_Divide_Long(vehicle_tonnage, 200)
      set lng_AI_Design_Max_Num_Primary_PD                 := Sys_Divide_Long(vehicle_tonnage, 400)

    "Ship (Bombardment)":
      set lng_AI_Design_Pct_Primary_Armor                  := 2 + armor_mod_pct
      set lng_AI_Design_Pct_Secondary_Armor                := 5
      set lng_AI_Design_Pct_Shields                        := 10 + shield_mod_pct

      set bool_AI_Design_Add_Scattering_Armor              := TRUE
      set bool_AI_Design_Add_Primary_Weapon                := TRUE
      set bool_AI_Design_Add_Primary_PD                    := (vehicle_tonnage > 300)
      set bool_AI_Design_Add_Supply_Storage                := bool_Enemy_Uses_Only_Engine_Weapons
      set bool_AI_Design_Add_Combat_Sensors                := TRUE
      set bool_AI_Design_Add_ECM                           := TRUE

      set lng_AI_Design_Max_Num_Primary_Weapon             := Sys_Divide_Long(vehicle_tonnage, 250)
      set lng_AI_Design_Max_Num_Primary_PD                 := 1

    "Ship (Plague)":
      set lng_AI_Design_Pct_Primary_Armor                  := 5 + armor_mod_pct
      set lng_AI_Design_Pct_Shields                        := 10 + shield_mod_pct

      set bool_AI_Design_Add_Scattering_Armor              := TRUE
      set bool_AI_Design_Add_Primary_PD                    := TRUE
      set bool_AI_Design_Add_Supply_Storage                := bool_Enemy_Uses_Only_Engine_Weapons
      set bool_AI_Design_Add_Combat_Sensors                := TRUE
      set bool_AI_Design_Add_ECM                           := TRUE

      set lng_AI_Design_Max_Num_Primary_PD                 := Sys_Divide_Long(vehicle_tonnage, 200)

    "Ship (Explorer)":
      set lng_AI_Design_Pct_Primary_Armor                  := 2 + armor_mod_pct
      set lng_AI_Design_Pct_Secondary_Armor                := 4
      set lng_AI_Design_Pct_Shields                        := 10 + shield_mod_pct

      set bool_AI_Design_Add_Supply_Storage                := TRUE
      set lng_AI_Design_Pct_Supply_Storage                 := 35

      if Is_Component_Available("Solar Collector") then
        set bool_AI_Design_Add_Solar_Collector             := TRUE
        set lng_AI_Design_Pct_Solar_Collector              := 15
        set lng_AI_Design_Pct_Supply_Storage               := 15
      endif

      if Is_Component_Available("Quantum Reactor") or Is_Component_Available("Flux Pod") then
        set bool_AI_Design_Add_Supply_Generation           := TRUE
        set lng_AI_Design_Pct_Supply_Storage               := 4
        set bool_AI_Design_Add_Solar_Collector             := FALSE
      endif

      set bool_AI_Design_Add_Extra_Movement                := Is_Component_Available("Solar Sail") or Is_Component_Available("Subspace Field Emitter")
      set bool_AI_Design_Add_ECM                           := TRUE

      if (vehicle_tonnage >= 300) then
        set bool_AI_Design_Add_Long_Range_Scanners         := Is_Component_Available("Long Range Scanners")
      endif

      set bool_AI_Design_Add_No_Weapons                    := TRUE

    "Ship (Scout)":
      set lng_AI_Design_Pct_Primary_Armor                  := 2 + armor_mod_pct
      set lng_AI_Design_Pct_Secondary_Armor                := 4
      set lng_AI_Design_Pct_Shields                        := 10 + shield_mod_pct

      set bool_AI_Design_Add_Supply_Storage                := TRUE
      set lng_AI_Design_Pct_Supply_Storage                 := 15
      set bool_AI_Design_Skip_Ordnance_Storage             := TRUE

      if Is_Component_Available("Solar Collector") then
        set bool_AI_Design_Add_Solar_Collector             := TRUE
        set lng_AI_Design_Pct_Solar_Collector              := 8
        set lng_AI_Design_Pct_Supply_Storage               := 5
      endif

      if Is_Component_Available("Quantum Reactor") or Is_Component_Available("Flux Pod") then
        set bool_AI_Design_Add_Supply_Generation           := TRUE
        set lng_AI_Design_Pct_Supply_Storage               := 4
        set bool_AI_Design_Add_Solar_Collector             := FALSE
      endif

      set bool_AI_Design_Add_Combat_Sensors                := TRUE
      set bool_AI_Design_Add_ECM                           := TRUE

      set bool_AI_Design_Add_Primary_Weapon                := TRUE
      set lng_AI_Design_Max_Num_Primary_Weapon             := 1
      set bool_AI_Design_Add_Primary_PD                    := TRUE
      set lng_AI_Design_Max_Num_Primary_PD                 := 1

      if (vehicle_tonnage >= 300) then
        if (lng_AI_Categorization = AI_CATEGORY_AGGRESSIVE) or (lng_AI_Categorization = AI_CATEGORY_AGGRESSIVE) then
          set lng_AI_Design_Max_Num_Primary_Weapon         := 2
        else
          if Is_Component_Available("Long Range Scanners") then
            set bool_AI_Design_Add_Long_Range_Scanners     := TRUE
          endif
        endif
      endif

    "Ship (Patrol)":
      set lng_AI_Design_Pct_Primary_Armor                  := 2 + armor_mod_pct
      set lng_AI_Design_Pct_Secondary_Armor                := 5
      set lng_AI_Design_Pct_Shields                        := 10 + shield_mod_pct

      set bool_AI_Design_Add_Bulkheads                     := bool_Enemy_Uses_Skip_Armor_Weapons or bool_Enemy_Uses_Skip_All_Weapons
      set bool_AI_Design_Add_Primary_Weapon                := TRUE
      set bool_AI_Design_Add_Primary_PD                    := TRUE
      set bool_AI_Design_Add_Supply_Storage                := bool_Enemy_Uses_Only_Engine_Weapons
      set bool_AI_Design_Add_Sensors                       := TRUE
      set bool_AI_Design_Add_Combat_Sensors                := TRUE
      set bool_AI_Design_Add_ECM                           := TRUE

      set lng_AI_Design_Max_Num_Primary_PD                 := 1

    "Carrier (Fighter)":
      // Carrier launcher and cargo bay usage in Rx[Add_Required_Components_To_Design]
      set lng_AI_Design_Pct_Primary_Armor                  := 2 + armor_mod_pct
      set lng_AI_Design_Pct_Secondary_Armor                := 5
      set lng_AI_Design_Pct_Shields                        := 10 + shield_mod_pct
      set lng_AI_Design_Pct_Supply_Storage                 := 5
      set lng_AI_Design_Pct_Ordnance_Storage               := 3

      set bool_AI_Design_Add_Supply_Storage                := TRUE
      set bool_AI_Design_Add_Ordnance_Storage              := TRUE
      set bool_AI_Design_Add_Supply_Generation             := TRUE
      set bool_AI_Design_Add_Primary_PD                    := TRUE
      set bool_AI_Design_Add_Secondary_PD                  := TRUE
      set bool_AI_Design_Add_Combat_Sensors                := TRUE
      set bool_AI_Design_Add_ECM                           := TRUE
      set bool_AI_Design_Add_No_Weapons                    := TRUE

      if (vehicle_tonnage < 1000) then
        set lng_AI_Design_Max_Num_Primary_PD               := 1
        set lng_AI_Design_Max_Num_Secondary_PD             := 1
      else
        set lng_AI_Design_Max_Num_Primary_PD               := 2
        set lng_AI_Design_Max_Num_Secondary_PD             := 2
      endif

    "Carrier (Drone)":
      // Carrier launcher and cargo bay usage in Rx[Add_Required_Components_To_Design]
      set lng_AI_Design_Pct_Primary_Armor                  := 2 + armor_mod_pct
      set lng_AI_Design_Pct_Secondary_Armor                := 5
      set lng_AI_Design_Pct_Shields                        := 10 + shield_mod_pct
      set lng_AI_Design_Pct_Supply_Storage                 := 5
      set lng_AI_Design_Pct_Ordnance_Storage               := 3

      set bool_AI_Design_Add_Supply_Generation             := TRUE
      set bool_AI_Design_Add_Supply_Storage                := TRUE
      set bool_AI_Design_Add_Ordnance_Storage              := TRUE
      set bool_AI_Design_Add_Primary_PD                    := TRUE
      set bool_AI_Design_Add_Secondary_PD                  := TRUE
      set bool_AI_Design_Add_Combat_Sensors                := TRUE
      set bool_AI_Design_Add_ECM                           := TRUE
      set bool_AI_Design_Add_No_Weapons                    := TRUE

      if (vehicle_tonnage < 1000) then
        set lng_AI_Design_Max_Num_Primary_PD               := 1
        set lng_AI_Design_Max_Num_Secondary_PD             := 1
      else
        set lng_AI_Design_Max_Num_Primary_PD               := 2
        set lng_AI_Design_Max_Num_Secondary_PD             := 2
      endif

    "Ship (Repair)":
      set lng_AI_Design_Pct_Primary_Armor                  := 10
      set lng_AI_Design_Pct_Supply_Storage                 := 5
      set lng_AI_Design_Pct_Ordnance_Storage               := 5

      set bool_AI_Design_Add_Supply_Storage                := TRUE
      set bool_AI_Design_Add_Ordnance_Storage              := bool_Race_Uses_Seeker_Ships

      if Is_Component_Available("Quantum Reactor") or Is_Component_Available("Flux Pod") then
        set bool_AI_Design_Add_Supply_Generation           := TRUE
      else
        set bool_AI_Design_Add_Solar_Collector             := TRUE
        set lng_AI_Design_Pct_Solar_Collector              := 5
      endif

    "Ship (Space Yard)":
      set lng_AI_Design_Pct_Primary_Armor                  := 5
      set lng_AI_Design_Pct_Supply_Storage                 := 5

      set bool_AI_Design_Add_Repair_Bay                    := (vehicle_tonnage > 800)

    "Colonizer (Rock)":
      set lng_AI_Design_Pct_Primary_Armor                  := 5
      set lng_AI_Design_Pct_Cargo_Bay                      := 10

      set bool_AI_Design_Add_Supply_Storage                := TRUE
      set lng_AI_Design_Pct_Supply_Storage                 := 10
      set bool_AI_Design_Add_No_Weapons                    := TRUE
      set bool_AI_Design_Add_Self_Destruct                 := TRUE

      if (bool_Enemy_Uses_Boarding_Parties) then
        set lng_AI_Design_Pct_Shields                      := 10
        set lng_AI_Design_Pct_Cargo_Bay                    := 0
        set bool_AI_Design_Add_Shield_Regenerator          := (vehicle_tonnage > 400)
        set bool_AI_Design_Add_Security_Station            := (vehicle_tonnage > 400)
      endif

    "Colonizer (Ice)":
      set lng_AI_Design_Pct_Primary_Armor                  := 5
      set lng_AI_Design_Pct_Cargo_Bay                      := 10

      set bool_AI_Design_Add_Supply_Storage                := TRUE
      set lng_AI_Design_Pct_Supply_Storage                 := 10
      set bool_AI_Design_Add_No_Weapons                    := TRUE
      set bool_AI_Design_Add_Self_Destruct                 := TRUE

      if (bool_Enemy_Uses_Boarding_Parties) then
        set lng_AI_Design_Pct_Shields                      := 10
        set lng_AI_Design_Pct_Cargo_Bay                    := 0
        set bool_AI_Design_Add_Shield_Regenerator          := (vehicle_tonnage > 400)
        set bool_AI_Design_Add_Security_Station            := (vehicle_tonnage > 400)
      endif

    "Colonizer (Gas)":
      set lng_AI_Design_Pct_Primary_Armor                  := 5
      set lng_AI_Design_Pct_Cargo_Bay                      := 10
      set lng_AI_Design_Pct_Supply_Storage                 := 5

      set bool_AI_Design_Add_Supply_Storage                := TRUE
      set lng_AI_Design_Pct_Supply_Storage                 := 10
      set bool_AI_Design_Add_No_Weapons                    := TRUE
      set bool_AI_Design_Add_Self_Destruct                 := TRUE

      if (bool_Enemy_Uses_Boarding_Parties) then
        set lng_AI_Design_Pct_Shields                      := 10
        set lng_AI_Design_Pct_Cargo_Bay                    := 0
        set bool_AI_Design_Add_Shield_Regenerator          := (vehicle_tonnage > 400)
        set bool_AI_Design_Add_Security_Station            := (vehicle_tonnage > 400)
      endif

    "Ship (Attack Base)":
      set lng_AI_Design_Pct_Primary_Armor                  := 2 + armor_mod_pct
      set lng_AI_Design_Pct_Secondary_Armor                := 5
      set lng_AI_Design_Pct_Shields                        := 10 + shield_mod_pct

      set bool_AI_Design_Add_Bulkheads                     := bool_Enemy_Uses_Skip_Armor_Weapons or bool_Enemy_Uses_Skip_All_Weapons
      set bool_AI_Design_Add_Stealth_Armor                 := TRUE
      set bool_AI_Design_Add_Scattering_Armor              := TRUE
      set bool_AI_Design_Add_Energy_Refractor              := bool_Enemy_Uses_Direct_Fire_Weapons
      set bool_AI_Design_Add_Primary_Weapon                := TRUE
      set bool_AI_Design_Add_Secondary_Weapon              := TRUE
      set bool_AI_Design_Add_Heavy_Weapon                  := TRUE
      set bool_AI_Design_Add_Special_Weapon                := TRUE
      set bool_AI_Design_Add_Primary_PD                    := TRUE
      set bool_AI_Design_Add_Secondary_PD                  := TRUE
      set bool_AI_Design_Add_Special_PD                    := TRUE
      set bool_AI_Design_Add_Supply_Storage                := bool_Enemy_Uses_Only_Engine_Weapons
      set bool_AI_Design_Add_Deflector_Array               := TRUE
      set bool_AI_Design_Add_Combat_Sensors                := TRUE
      set bool_AI_Design_Add_ECM                           := TRUE
      set bool_AI_Design_Add_Scanner_Jammer                := bool_Enemy_Uses_Long_Range_Scanners
      set bool_AI_Design_Add_Stealth_Totem                 := bool_Race_Uses_Religious_Tech
      set bool_AI_Design_Add_Talisman                      := bool_Race_Uses_Religious_Tech
      set bool_AI_Design_Add_Holy_War_Totem                := bool_Race_Uses_Religious_Tech
      set bool_AI_Design_Add_Vengeance_Totem               := bool_Race_Uses_Religious_Tech
      set bool_AI_Design_Add_Supply_Generation             := TRUE
      set bool_AI_Design_Add_Shield_Regenerator            := (lng_AI_Design_Pct_Shields > 0)
      set bool_AI_Design_Add_Cloaking_Device               := bool_Race_Uses_Cloaking_Devices
      set bool_AI_Design_Add_Extra_Movement                := TRUE
      set bool_AI_Design_Add_Combat_Movement               := TRUE
      set bool_AI_Design_Add_Security_Station              := bool_Enemy_Uses_Boarding_Parties
      set bool_AI_Design_Add_Self_Destruct                 := bool_Enemy_Uses_Boarding_Parties

      set lng_AI_Design_Max_Num_Secondary_Weapon           := 2
      set lng_AI_Design_Max_Num_Special_Weapon             := 2
      set lng_AI_Design_Max_Num_Primary_PD                 := 2 + pd_mod_amt
      set lng_AI_Design_Max_Num_Secondary_PD               := 2 + pd_mod_amt
      set lng_AI_Design_Max_Num_Special_PD                 := 1 + pd_mod_amt

    "Base (Defense)":
      set lng_AI_Design_Pct_Primary_Armor                  := 5 + armor_mod_pct
      set lng_AI_Design_Pct_Secondary_Armor                := 5
      set lng_AI_Design_Pct_Shields                        := 15 + shield_mod_pct
      set lng_AI_Design_Pct_Supply_Storage                 := 5

      set bool_AI_Design_Add_Supply_Storage                := TRUE
      set bool_AI_Design_Add_Ordnance_Storage              := TRUE

      set bool_AI_Design_Add_Bulkheads                     := bool_Enemy_Uses_Skip_Armor_Weapons or bool_Enemy_Uses_Skip_All_Weapons
      set bool_AI_Design_Add_Stealth_Armor                 := TRUE
      set bool_AI_Design_Add_Scattering_Armor              := TRUE
      set bool_AI_Design_Add_Energy_Refractor              := bool_Enemy_Uses_Direct_Fire_Weapons
      set bool_AI_Design_Add_Primary_Weapon                := TRUE
      set bool_AI_Design_Add_Secondary_Weapon              := TRUE
      set bool_AI_Design_Add_Heavy_Weapon                  := TRUE
      set bool_AI_Design_Add_Special_Weapon                := TRUE
      set bool_AI_Design_Add_Primary_PD                    := TRUE
      set bool_AI_Design_Add_Secondary_PD                  := TRUE
      set bool_AI_Design_Add_Deflector_Array               := (vehicle_tonnage > 700)
      set bool_AI_Design_Add_Combat_Sensors                := TRUE
      set bool_AI_Design_Add_ECM                           := TRUE
      set bool_AI_Design_Add_Long_Range_Scanners           := TRUE
      set bool_AI_Design_Add_Scanner_Jammer                := bool_Enemy_uses_Long_Range_Scanners
      set bool_AI_Design_Add_Stealth_Totem                 := bool_Race_Uses_Religious_Tech
      set bool_AI_Design_Add_Talisman                      := bool_Race_Uses_Religious_Tech
      set bool_AI_Design_Add_Holy_War_Totem                := bool_Race_Uses_Religious_Tech
      set bool_AI_Design_Add_Vengeance_Totem               := bool_Race_Uses_Religious_Tech

      if Is_Component_Available("Quantum Reactor") or Is_Component_Available("Flux Pod") then
        set lng_AI_Design_Pct_Supply_Generation            := 3
        set lng_AI_Design_Pct_Supply_Storage               := 2
      else
        set lng_AI_Design_Pct_Solar_Collector              := 4
      endif

      set bool_AI_Design_Add_Shield_Regenerator            := (lng_AI_Design_Pct_Shields > 0)
      set bool_AI_Design_Add_Security_Station              := bool_Enemy_Uses_Boarding_Parties and (vehicle_tonnage > 1000)
      set bool_AI_Design_Add_Self_Destruct                 := TRUE

      set lng_AI_Design_Max_Num_Secondary_Weapon           := 4
      set lng_AI_Design_Max_Num_Special_Weapon             := 4
      set lng_AI_Design_Max_Num_Primary_PD                 := 4
      set lng_AI_Design_Max_Num_Secondary_PD               := 4
      set lng_AI_Design_Max_Num_Special_PD                 := 1 + pd_mod_amt

    "Base (Space Yard)":
      set lng_AI_Design_Pct_Primary_Armor                  := 5

      set bool_AI_Design_Add_Supply_Storage                := TRUE
      set lng_AI_Design_Pct_Supply_Storage                 := 1

      set bool_AI_Design_Add_Repair_Bay                    := (vehicle_tonnage >= 700)
      set lng_AI_Design_Pct_Repair_Bay                     := 40

      set bool_AI_Design_Add_No_Weapons                    := (vehicle_tonnage < 800)
      set bool_AI_Design_Add_Primary_Weapon                := (vehicle_tonnage >= 800)
      set bool_AI_Design_Add_Primary_PD                    := TRUE
      set bool_AI_Design_Add_Secondary_PD                  := (vehicle_tonnage >= 800)

      set lng_AI_Design_Max_Num_Primary_Weapon             := 1
      set lng_AI_Design_Max_Num_Primary_PD                 := 1
      set lng_AI_Design_Max_Num_Secondary_PD               := 1

    "Ship (Medical)":
      set lng_AI_Design_Pct_Primary_Armor                  := 5
      set lng_AI_Design_Pct_Shields                        := 15
      set lng_AI_Design_Pct_Supply_Storage                 := 5

      set bool_AI_Design_Add_Medical_Bay                   := TRUE
      set bool_AI_Design_Add_No_Weapons                    := TRUE

    "Transport (Population)":
      set lng_AI_Design_Pct_Primary_Armor                  := 5

      set bool_AI_Design_Add_Supply_Storage                := TRUE
      set lng_AI_Design_Pct_Supply_Storage                 := 5
      set lng_AI_Design_Pct_Cargo_Bay                      := 60
      set bool_AI_Design_Add_No_Weapons                    := TRUE

    "Transport (Troop)":
      set lng_AI_Design_Pct_Primary_Armor                  := 4 + armor_mod_pct
      set lng_AI_Design_Pct_Secondary_Armor                := 4
      set lng_AI_Design_Pct_Shields                        := 6 + shield_mod_pct
      set lng_AI_Design_Pct_Supply_Storage                 := 3
      set lng_AI_Design_Pct_Cargo_Bay                      := 50

      set bool_AI_Design_Add_Combat_Movement               := (vehicle_tonnage >= 1000)
      set bool_AI_Design_Add_Primary_Weapon                := (vehicle_tonnage >= 300)

      set lng_AI_Design_Max_Num_Primary_Weapon             := Sys_Divide_Long(vehicle_tonnage, 500)
      set lng_AI_Design_Max_Num_Primary_PD                 := Sys_Divide_Long(vehicle_tonnage, 300)

    "Transport (Cargo)":
      set lng_AI_Design_Pct_Primary_Armor                  := 5
      set lng_AI_Design_Pct_Cargo_Bay                      := 60
      set bool_AI_Design_Add_No_Weapons                    := TRUE

    "Transport (Supply)":
      set lng_AI_Design_Pct_Primary_Armor                  := 5

      if (bool_Race_Uses_Seeker_Ships) then
        set lng_AI_Design_Pct_Supply_Storage               := 30
        set lng_AI_Design_Pct_Ordnance_Storage             := 30
      else
        set lng_AI_Design_Pct_Supply_Storage               := 40
        set lng_AI_Design_Pct_Ordnance_Storage             := 20
      endif

      if Is_Component_Available("Quantum Reactor") or Is_Component_Available("Flux Pod") then
        set bool_AI_Design_Add_Supply_Generation           := TRUE
        set lng_AI_Design_Pct_Supply_Generation            := 5
      else
        set bool_AI_Design_Add_Solar_Collector             := TRUE
        set lng_AI_Design_Pct_Solar_Collector              := 10
      endif

      if Is_Component_Available("Ordnance Replicator") then
        set bool_AI_Design_Add_Ordnance_Replicator         := TRUE
        set lng_AI_Design_Pct_Ordnance_Replicator          := Sys_Trunc(lng_AI_Design_Pct_Ordnance_Storage * 0.2)
        set lng_AI_Design_Pct_Ordnance_Storage             := Sys_Trunc(lng_AI_Design_Pct_Ordnance_Storage * 0.8)
      endif

      set bool_AI_Design_Add_Supply_Storage                := TRUE
      set bool_AI_Design_Add_Ordnance_Storage              := TRUE
      set bool_AI_Design_Add_No_Weapons                    := TRUE

    "Ship (Remote Mining)":
      set lng_AI_Design_Pct_Primary_Armor                  := 5

      if Is_Component_Available("Quantum Reactor") or Is_Component_Available("Flux Pod") then
        set bool_AI_Design_Add_Supply_Generation           := TRUE
      else
        set bool_AI_Design_Add_Solar_Collector             := TRUE
        set lng_AI_Design_Pct_Solar_Collector              := 10
      endif

      set bool_AI_Design_Add_No_Weapons                    := TRUE

    "Ship (Remote Farming)":
      set lng_AI_Design_Pct_Primary_Armor                  := 5

      if Is_Component_Available("Quantum Reactor") or Is_Component_Available("Flux Pod") then
        set bool_AI_Design_Add_Supply_Generation           := TRUE
      else
        set bool_AI_Design_Add_Solar_Collector             := TRUE
        set lng_AI_Design_Pct_Solar_Collector              := 10
      endif

      set bool_AI_Design_Add_No_Weapons                    := TRUE

    "Ship (Remote Refining)":
      set lng_AI_Design_Pct_Primary_Armor                  := 5

      if Is_Component_Available("Quantum Reactor") or Is_Component_Available("Flux Pod") then
        set bool_AI_Design_Add_Supply_Generation           := TRUE
      else
        set bool_AI_Design_Add_Solar_Collector             := TRUE
        set lng_AI_Design_Pct_Solar_Collector              := 10
      endif

      set bool_AI_Design_Add_No_Weapons                    := TRUE

    "Ship (Mine Layer)":
      set lng_AI_Design_Pct_Primary_Armor                  := 5
      set lng_AI_Design_Pct_Supply_Storage                 := 3
      set lng_AI_Design_Pct_Cargo_Bay                      := 60

      set bool_AI_Design_Add_No_Weapons                    := TRUE

    "Ship (Mine Sweeper)":
      set lng_AI_Design_Pct_Primary_Armor                  := 5
      set lng_AI_Design_Pct_Supply_Storage                 := 5

      set bool_AI_Design_Add_No_Weapons                    := TRUE
      set bool_AI_Design_Add_Primary_PD                    := bool_Enemy_Uses_Seeker_Weapons

      set lng_AI_Design_Max_Num_Primary_PD                 := 1

    "Ship (Satellite Launcher)":
      set lng_AI_Design_Pct_Primary_Armor                  := 5
      set lng_AI_Design_Pct_Supply_Storage                 := 3
      set lng_AI_Design_Pct_Cargo_Bay                      := 60

      set bool_AI_Design_Add_No_Weapons                    := TRUE

    "Ship (Warp Point Opener)":
      set lng_AI_Design_Pct_Primary_Armor                  := 10 + armor_mod_pct
      set lng_AI_Design_Pct_Shields                        := 10 + shield_mod_pct
      set lng_AI_Design_Pct_Supply_Storage                 := 5

      set bool_AI_Design_Add_Scattering_Armor              := Is_Component_Available("Scattering Armor")
      set bool_AI_Design_Add_Supply_Generation             := Is_Component_Available("Quantum Reactor") or Is_Component_Available("Flux Pod")
      set bool_AI_Design_Add_Repair_Bay                    := (vehicle_tonnage >= 600)
      set bool_AI_Design_Add_Scanner_Jammer                := bool_Enemy_Uses_Long_Range_Scanners
      set bool_AI_Design_Add_Self_Destruct                 := TRUE
      set bool_AI_Design_Add_Deflector_Array               := TRUE
      set bool_AI_Design_Add_No_Weapons                    := TRUE

    "Ship (Warp Point Closer)":
      set lng_AI_Design_Pct_Primary_Armor                  := 10 + armor_mod_pct
      set lng_AI_Design_Pct_Shields                        := 10 + shield_mod_pct
      set lng_AI_Design_Pct_Supply_Storage                 := 5

      set bool_AI_Design_Add_Scattering_Armor              := Is_Component_Available("Scattering Armor")
      set bool_AI_Design_Add_Supply_Generation             := Is_Component_Available("Quantum Reactor") or Is_Component_Available("Flux Pod")
      set bool_AI_Design_Add_Repair_Bay                    := (vehicle_tonnage >= 600)
      set bool_AI_Design_Add_Scanner_Jammer                := bool_Enemy_Uses_Long_Range_Scanners
      set bool_AI_Design_Add_Self_Destruct                 := TRUE
      set bool_AI_Design_Add_Deflector_Array               := TRUE
      set bool_AI_Design_Add_No_Weapons                    := TRUE

    "Ship (Planet Creator)":
      set lng_AI_Design_Pct_Primary_Armor                  := 10 + armor_mod_pct
      set lng_AI_Design_Pct_Shields                        := 10 + shield_mod_pct
      set lng_AI_Design_Pct_Supply_Storage                 := 5

      set bool_AI_Design_Add_Scattering_Armor              := Is_Component_Available("Scattering Armor")
      set bool_AI_Design_Add_Supply_Generation             := Is_Component_Available("Quantum Reactor") or Is_Component_Available("Flux Pod")
      set bool_AI_Design_Add_Repair_Bay                    := (vehicle_tonnage >= 600)
      set bool_AI_Design_Add_Scanner_Jammer                := bool_Enemy_Uses_Long_Range_Scanners
      set bool_AI_Design_Add_Self_Destruct                 := TRUE
      set bool_AI_Design_Add_Deflector_Array               := TRUE
      set bool_AI_Design_Add_No_Weapons                    := TRUE

    "Ship (Planet Destroyer)":
      set lng_AI_Design_Pct_Primary_Armor                  := 10 + armor_mod_pct
      set lng_AI_Design_Pct_Shields                        := 10 + shield_mod_pct
      set lng_AI_Design_Pct_Supply_Storage                 := 5

      set bool_AI_Design_Add_Scattering_Armor              := Is_Component_Available("Scattering Armor")
      set bool_AI_Design_Add_Supply_Generation             := Is_Component_Available("Quantum Reactor") or Is_Component_Available("Flux Pod")
      set bool_AI_Design_Add_Cloaking_Device               := Is_Component_Available("Cloaking Device")
      set bool_AI_Design_Add_Repair_Bay                    := (vehicle_tonnage >= 600)
      set bool_AI_Design_Add_Scanner_Jammer                := TRUE
      set bool_AI_Design_Add_Self_Destruct                 := TRUE
      set bool_AI_Design_Add_Deflector_Array               := TRUE
      set bool_AI_Design_Add_No_Weapons                    := TRUE

    "Ship (Star Creator)":
      set lng_AI_Design_Pct_Primary_Armor                  := 10 + armor_mod_pct
      set lng_AI_Design_Pct_Shields                        := 10 + shield_mod_pct
      set lng_AI_Design_Pct_Supply_Storage                 := 5

      set bool_AI_Design_Add_Scattering_Armor              := Is_Component_Available("Scattering Armor")
      set bool_AI_Design_Add_Supply_Generation             := Is_Component_Available("Quantum Reactor") or Is_Component_Available("Flux Pod")
      set bool_AI_Design_Add_Repair_Bay                    := (vehicle_tonnage >= 600)
      set bool_AI_Design_Add_Scanner_Jammer                := bool_Enemy_Uses_Long_Range_Scanners
      set bool_AI_Design_Add_Self_Destruct                 := TRUE
      set bool_AI_Design_Add_Deflector_Array               := TRUE
      set bool_AI_Design_Add_No_Weapons                    := TRUE

    "Ship (Star Destroyer)":
      set lng_AI_Design_Pct_Primary_Armor                  := 10 + armor_mod_pct
      set lng_AI_Design_Pct_Shields                        := 10 + shield_mod_pct
      set lng_AI_Design_Pct_Supply_Storage                 := 5

      set bool_AI_Design_Add_Scattering_Armor              := Is_Component_Available("Scattering Armor")
      set bool_AI_Design_Add_Supply_Generation             := Is_Component_Available("Quantum Reactor") or Is_Component_Available("Flux Pod")
      set bool_AI_Design_Add_Cloaking_Device               := Is_Component_Available("Cloaking Device")
      set bool_AI_Design_Add_Repair_Bay                    := (vehicle_tonnage >= 600)
      set bool_AI_Design_Add_Scanner_Jammer                := TRUE
      set bool_AI_Design_Add_Self_Destruct                 := TRUE
      set bool_AI_Design_Add_Deflector_Array               := TRUE
      set bool_AI_Design_Add_No_Weapons                    := TRUE

    "Ship (Storm Creator)":
      set lng_AI_Design_Pct_Primary_Armor                  := 10 + armor_mod_pct
      set lng_AI_Design_Pct_Shields                        := 10 + shield_mod_pct
      set lng_AI_Design_Pct_Supply_Storage                 := 5

      set bool_AI_Design_Add_Scattering_Armor              := Is_Component_Available("Scattering Armor")
      set bool_AI_Design_Add_Supply_Generation             := Is_Component_Available("Quantum Reactor") or Is_Component_Available("Flux Pod")
      set bool_AI_Design_Add_Cloaking_Device               := Is_Component_Available("Cloaking Device")
      set bool_AI_Design_Add_Repair_Bay                    := (vehicle_tonnage >= 600)
      set bool_AI_Design_Add_Scanner_Jammer                := bool_Enemy_Uses_Long_Range_Scanners
      set bool_AI_Design_Add_Self_Destruct                 := TRUE
      set bool_AI_Design_Add_Deflector_Array               := TRUE
      set bool_AI_Design_Add_No_Weapons                    := TRUE

    "Ship (Storm Destroyer)":
      set lng_AI_Design_Pct_Primary_Armor                  := 10 + armor_mod_pct
      set lng_AI_Design_Pct_Shields                        := 10 + shield_mod_pct
      set lng_AI_Design_Pct_Supply_Storage                 := 5

      set bool_AI_Design_Add_Scattering_Armor              := Is_Component_Available("Scattering Armor")
      set bool_AI_Design_Add_Supply_Generation             := Is_Component_Available("Quantum Reactor") or Is_Component_Available("Flux Pod")
      set bool_AI_Design_Add_Repair_Bay                    := (vehicle_tonnage >= 600)
      set bool_AI_Design_Add_Scanner_Jammer                := bool_Enemy_Uses_Long_Range_Scanners
      set bool_AI_Design_Add_Self_Destruct                 := TRUE
      set bool_AI_Design_Add_Deflector_Array               := TRUE
      set bool_AI_Design_Add_No_Weapons                    := TRUE

    "Ship (Black Hole Creator)":
      set lng_AI_Design_Pct_Primary_Armor                  := 10 + armor_mod_pct
      set lng_AI_Design_Pct_Shields                        := 10 + shield_mod_pct
      set lng_AI_Design_Pct_Supply_Storage                 := 5

      set bool_AI_Design_Add_Scattering_Armor              := Is_Component_Available("Scattering Armor")
      set bool_AI_Design_Add_Supply_Generation             := Is_Component_Available("Quantum Reactor") or Is_Component_Available("Flux Pod")
      set bool_AI_Design_Add_Cloaking_Device               := Is_Component_Available("Cloaking Device")
      set bool_AI_Design_Add_Repair_Bay                    := (vehicle_tonnage >= 600)
      set bool_AI_Design_Add_Scanner_Jammer                := TRUE
      set bool_AI_Design_Add_Self_Destruct                 := TRUE
      set bool_AI_Design_Add_Deflector_Array               := TRUE
      set bool_AI_Design_Add_No_Weapons                    := TRUE

    "Ship (Black Hole Destroyer)":
      set lng_AI_Design_Pct_Primary_Armor                  := 10 + armor_mod_pct
      set lng_AI_Design_Pct_Shields                        := 10 + shield_mod_pct
      set lng_AI_Design_Pct_Supply_Storage                 := 5

      set bool_AI_Design_Add_Scattering_Armor              := Is_Component_Available("Scattering Armor")
      set bool_AI_Design_Add_Supply_Generation             := Is_Component_Available("Quantum Reactor") or Is_Component_Available("Flux Pod")
      set bool_AI_Design_Add_Repair_Bay                    := (vehicle_tonnage >= 600)
      set bool_AI_Design_Add_Scanner_Jammer                := bool_Enemy_Uses_Long_Range_Scanners
      set bool_AI_Design_Add_Self_Destruct                 := TRUE
      set bool_AI_Design_Add_Deflector_Array               := TRUE
      set bool_AI_Design_Add_No_Weapons                    := TRUE
      
    "Ship (Nebulae Creator)":
      set lng_AI_Design_Pct_Primary_Armor                  := 10 + armor_mod_pct
      set lng_AI_Design_Pct_Shields                        := 10 + shield_mod_pct
      set lng_AI_Design_Pct_Supply_Storage                 := 5

      set bool_AI_Design_Add_Scattering_Armor              := Is_Component_Available("Scattering Armor")
      set bool_AI_Design_Add_Supply_Generation             := Is_Component_Available("Quantum Reactor") or Is_Component_Available("Flux Pod")
      set bool_AI_Design_Add_Cloaking_Device               := Is_Component_Available("Cloaking Device")
      set bool_AI_Design_Add_Repair_Bay                    := (vehicle_tonnage >= 600)
      set bool_AI_Design_Add_Scanner_Jammer                := TRUE
      set bool_AI_Design_Add_Self_Destruct                 := TRUE
      set bool_AI_Design_Add_Deflector_Array               := TRUE
      set bool_AI_Design_Add_No_Weapons                    := TRUE

    "Ship (Nebulae Destroyer)":
      set lng_AI_Design_Pct_Primary_Armor                  := 10 + armor_mod_pct
      set lng_AI_Design_Pct_Shields                        := 10 + shield_mod_pct
      set lng_AI_Design_Pct_Supply_Storage                 := 5

      set bool_AI_Design_Add_Scattering_Armor              := Is_Component_Available("Scattering Armor")
      set bool_AI_Design_Add_Supply_Generation             := Is_Component_Available("Quantum Reactor") or Is_Component_Available("Flux Pod")
      set bool_AI_Design_Add_Repair_Bay                    := (vehicle_tonnage >= 600)
      set bool_AI_Design_Add_Scanner_Jammer                := bool_Enemy_Uses_Long_Range_Scanners
      set bool_AI_Design_Add_Self_Destruct                 := TRUE
      set bool_AI_Design_Add_Deflector_Array               := TRUE
      set bool_AI_Design_Add_No_Weapons                    := TRUE
      
    "Troop":
      set lng_AI_Design_Pct_Primary_Armor                  := 10
      set lng_AI_Design_Pct_Secondary_Armor                := 5

      set bool_AI_Design_Add_Shields                       := (vehicle_tonnage >= 20)
      set bool_AI_Design_Add_Primary_Weapon                := TRUE
      set bool_AI_Design_Add_Secondary_Weapon              := (vehicle_tonnage >= 20)
      set bool_AI_Design_Add_Supply_Storage                := (vehicle_tonnage >= 20)
      set bool_AI_Design_Add_Combat_Sensors                := (vehicle_tonnage >= 20)
      set bool_AI_Design_Add_ECM                           := (vehicle_tonnage >= 20)
      set bool_AI_Design_Add_Turbocharger                  := (vehicle_tonnage > 20)

      set lng_AI_Design_Max_Num_Secondary_Weapon           := 1

    "Troop (Anti-Fighter)":
      set lng_AI_Design_Pct_Primary_Armor                  := 10
      set lng_AI_Design_Pct_Secondary_Armor                := 5

      set bool_AI_Design_Add_Shields                       := (vehicle_tonnage >= 20)
      set bool_AI_Design_Add_Primary_Weapon                := TRUE
      set bool_AI_Design_Add_Secondary_Weapon              := (vehicle_tonnage >= 20)
      set bool_AI_Design_Add_Supply_Storage                := (vehicle_tonnage >= 20)
      set bool_AI_Design_Add_Combat_Sensors                := (vehicle_tonnage >= 20)
      set bool_AI_Design_Add_ECM                           := (vehicle_tonnage >= 20)
      set bool_AI_Design_Add_Turbocharger                  := (vehicle_tonnage > 20)

      set lng_AI_Design_Max_Num_Secondary_Weapon           := 1

    "Troop (Light)":
      set lng_AI_Design_Pct_Primary_Armor                  := 10

      set bool_AI_Design_Add_Primary_Weapon                := TRUE
      set bool_AI_Design_Add_Combat_Sensors                := TRUE

    "Troop (Heavy)":
      set lng_AI_Design_Pct_Primary_Armor                  := 10
      set lng_AI_Design_Pct_Secondary_Armor                := 5

      set bool_AI_Design_Add_Shields                       := (vehicle_tonnage >= 20)
      set bool_AI_Design_Add_Heavy_Weapon                  := TRUE
      set bool_AI_Design_Add_Supply_Storage                := TRUE
      set bool_AI_Design_Add_Combat_Sensors                := TRUE
      set bool_AI_Design_Add_ECM                           := TRUE
      set bool_AI_Design_Add_Turbocharger                  := (vehicle_tonnage >= 20)

    "Fighter":
      set lng_AI_Design_Pct_Primary_Armor                  := 8
      set lng_AI_Design_Pct_Secondary_Armor                := 4

      set bool_AI_Design_Add_Shields                       := TRUE
      set bool_AI_Design_Add_Primary_Weapon                := TRUE
      set bool_AI_Design_Add_Secondary_Weapon              := TRUE
      set bool_AI_Design_Add_Supply_Storage                := TRUE
      set bool_AI_Design_Add_Combat_Sensors                := TRUE
      set bool_AI_Design_Add_ECM                           := TRUE
      set bool_AI_Design_Add_Afterburners                  := (vehicle_tonnage > 25)

      set lng_AI_Design_Max_Num_Secondary_Weapon           := 2

    "Fighter (Anti-Fighter)":
      set lng_AI_Design_Pct_Primary_Armor                  := 8
      set lng_AI_Design_Pct_Secondary_Armor                := 4

      set bool_AI_Design_Add_Shields                       := TRUE
      set bool_AI_Design_Add_Primary_Weapon                := TRUE
      set bool_AI_Design_Add_Secondary_Weapon              := TRUE
      set bool_AI_Design_Add_Supply_Storage                := TRUE
      set bool_AI_Design_Add_Combat_Sensors                := TRUE
      set bool_AI_Design_Add_ECM                           := TRUE
      set bool_AI_Design_Add_Afterburners                  := TRUE

      set lng_AI_Design_Max_Num_Secondary_Weapon           := 2

    "Fighter (Bomber)":
      set lng_AI_Design_Pct_Primary_Armor                  := 10

      set bool_AI_Design_Add_Shields                       := TRUE
      set bool_AI_Design_Add_Heavy_Weapon                  := TRUE
      set bool_AI_Design_Add_Supply_Storage                := TRUE
      set bool_AI_Design_Add_Combat_Sensors                := TRUE
      set bool_AI_Design_Add_ECM                           := TRUE
      set bool_AI_Design_Add_Afterburners                  := (vehicle_tonnage > 25)

    "Fighter (Kamikaze)":
      set lng_AI_Design_Pct_Primary_Armor                  := 20
      set bool_AI_Design_Add_Afterburners                  := (vehicle_tonnage > 25)

    "Satellite":
      set lng_AI_Design_Pct_Primary_Armor                  := 10
      set lng_AI_Design_Pct_Secondary_Armor                := 10

      set bool_AI_Design_Add_Shields                       := (vehicle_tonnage = 150) and (bool_Enemy_Uses_Skip_Shield_Weapons)
      set bool_AI_Design_Add_Primary_Weapon                := TRUE
      set bool_AI_Design_Add_Primary_PD                    := (vehicle_tonnage > 120)
      set bool_AI_Design_Add_Supply_Storage                := (vehicle_tonnage > 120)
      set bool_AI_Design_Add_Combat_Sensors                := (vehicle_tonnage >= 90)
      set bool_AI_Design_Add_ECM                           := (vehicle_tonnage >= 100)

      set lng_AI_Design_Max_Num_Primary_PD                 := 1

    "Satellite (Point-Defense)":
      set lng_AI_Design_Pct_Primary_Armor                  := 5
      set lng_AI_Design_Pct_Secondary_Armor                := 5

      set bool_AI_Design_Add_Primary_PD                    := TRUE
      set bool_AI_Design_Add_Secondary_PD                  := TRUE
      set bool_AI_Design_Add_Supply_Storage                := (vehicle_tonnage > 100)
      set bool_AI_Design_Add_Combat_Sensors                := TRUE

    "Satellite (Seeker)":
      set lng_AI_Design_Pct_Primary_Armor                  := armor_mod_pct
      set lng_AI_Design_Pct_Supply_Storage                 := 5
      set lng_AI_Design_Pct_Ordnance_Storage               := 5

      set bool_AI_Design_Add_Heavy_Weapon                  := TRUE
      set bool_AI_Design_Add_Ordnance_Storage              := TRUE
      set bool_AI_Design_Add_Supply_Storage                := (vehicle_tonnage >= 200)
      set bool_AI_Design_Add_ECM                           := (vehicle_tonnage >= 100)

    "Satellite (Recon)":
      set lng_AI_Design_Pct_Primary_Armor                  := 15

      set bool_AI_Design_Add_Supply_Storage                := TRUE
      set bool_AI_Design_Add_Sensors                       := TRUE
      set bool_AI_Design_Add_Long_Range_Scanners           := TRUE

    "Satellite (Repair)":
      set lng_AI_Design_Pct_Primary_Armor                  := 15

      set bool_AI_Design_Add_Supply_Storage                := TRUE
      set bool_AI_Design_Add_Repair_Pod                    := TRUE
      set bool_AI_Design_Add_No_Weapons                    := TRUE

    "Weapon Platform":
      set lng_AI_Design_Pct_Primary_Armor                  := 5 + Sys_Divide_Long(armor_mod_pct, 2)
      set lng_AI_Design_Pct_Secondary_Armor                := 5 + Sys_Divide_Long(armor_mod_pct, 2)
      set lng_AI_Design_Pct_Shields                        := 10 + shield_mod_pct

      set bool_AI_Design_Add_Primary_Weapon                := TRUE
      set bool_AI_Design_Add_Secondary_Weapon              := TRUE
      set bool_AI_Design_Add_Heavy_Weapon                  := TRUE
      set bool_AI_Design_Add_Special_Weapon                := TRUE
      set bool_AI_Design_Add_Primary_PD                    := TRUE
      set bool_AI_Design_Add_Secondary_PD                  := TRUE
      set bool_AI_Design_Add_Special_PD                    := (vehicle_tonnage > 250)
      set bool_AI_Design_Add_Combat_Sensors                := TRUE
      set bool_AI_Design_Add_ECM                           := TRUE

      set lng_AI_Design_Max_Num_Secondary_Weapon           := Sys_Divide_Long(vehicle_tonnage, 250)
      set lng_AI_Design_Max_Num_Special_Weapon             := Sys_Divide_Long(vehicle_tonnage, 500)

      if (bool_Enemy_Uses_Seeker_Weapons) or (bool_Enemy_Uses_Long_Range_Seeker_Weapons) then
        set lng_AI_Design_Max_Num_Primary_PD               := Sys_Divide_Long(vehicle_tonnage, 200)
        set lng_AI_Design_Max_Num_Secondary_PD             := Sys_Divide_Long(vehicle_tonnage, 200)
      else
        set lng_AI_Design_Max_Num_Primary_PD               := Sys_Divide_Long(vehicle_tonnage, 200)
        set lng_AI_Design_Max_Num_Secondary_PD             := Sys_Divide_Long(vehicle_tonnage, 400)
      endif

    "Weapon Platform (Seeker)":
      set lng_AI_Design_Pct_Primary_Armor                  := 5 + Sys_Divide_Long(armor_mod_pct, 2)
      set lng_AI_Design_Pct_Secondary_Armor                := 5 + Sys_Divide_Long(armor_mod_pct, 2)
      set lng_AI_Design_Pct_Shields                        := 10 + shield_mod_pct

      set bool_AI_Design_Add_Heavy_Weapon                  := TRUE
      set bool_AI_Design_Add_Primary_PD                    := TRUE
      set bool_AI_Design_Add_Secondary_PD                  := TRUE
      set bool_AI_Design_Add_Combat_Sensors                := TRUE
      set bool_AI_Design_Add_ECM                           := TRUE

      if (bool_Enemy_Uses_Seeker_Weapons) or (bool_Enemy_Uses_Long_Range_Seeker_Weapons) then
        set lng_AI_Design_Max_Num_Primary_PD               := 1 + Sys_Divide_Long(vehicle_tonnage, 250)
        set lng_AI_Design_Max_Num_Secondary_PD             := 1 + Sys_Divide_Long(vehicle_tonnage, 500)
        set lng_AI_Design_Max_Num_Special_PD               := Sys_Divide_Long(vehicle_tonnage, 500)
      else
        set lng_AI_Design_Max_Num_Primary_PD               := 1 + Sys_Divide_Long(vehicle_tonnage, 250)
        set lng_AI_Design_Max_Num_Secondary_PD             := 1
        set lng_AI_Design_Max_Num_Special_PD               := Sys_Divide_Long(vehicle_tonnage, 500)
      endif

    "Drone (Attack)":
      set lng_AI_Design_Pct_Primary_Armor                  := 5 + Sys_Divide_Long(armor_mod_pct, 2)
      set lng_AI_Design_Pct_Secondary_Armor                := 5 + Sys_Divide_Long(vehicle_tonnage, 100)

      set bool_AI_Design_Add_Shields                       := (vehicle_tonnage > 100)
      set bool_AI_Design_Add_Primary_Weapon                := TRUE
      set bool_AI_Design_Add_Secondary_Weapon              := TRUE
      set bool_AI_Design_Add_Primary_PD                    := (vehicle_tonnage > 100)
      set bool_AI_Design_Add_Supply_Storage                := bool_Enemy_Uses_Only_Engine_Weapons
      set bool_AI_Design_Add_Combat_Sensors                := TRUE
      set bool_AI_Design_Add_ECM                           := (vehicle_tonnage > 100)
      set bool_AI_Design_Add_Combat_Thruster               := (vehicle_tonnage > 100)

      set lng_AI_Design_Max_Num_Primary_PD                 := 1

    "Drone (Defense)":
      set lng_AI_Design_Pct_Primary_Armor                  := 5 + Sys_Divide_Long(armor_mod_pct, 2)
      set lng_AI_Design_Pct_Secondary_Armor                := Sys_Round(100 * 10 / vehicle_tonnage)

      set bool_AI_Design_Add_Shields                       := (vehicle_tonnage > 100)
      set bool_AI_Design_Add_Heavy_Weapon                  := TRUE
      set bool_AI_Design_Add_Primary_PD                    := (vehicle_tonnage > 100)
      set bool_AI_Design_Add_Supply_Storage                := bool_Enemy_Uses_Only_Engine_Weapons
      set bool_AI_Design_Add_Combat_Sensors                := (vehicle_tonnage > 100)
      set bool_AI_Design_Add_ECM                           := (vehicle_tonnage > 100)

      set lng_AI_Design_Max_Num_Heavy_Weapon               := 1
      set lng_AI_Design_Max_Num_Primary_PD                 := 1

    "Drone (Seeker)":
      set lng_AI_Design_Pct_Primary_Armor                  := 5 + Sys_Divide_Long(armor_mod_pct, 2)
      set lng_AI_Design_Pct_Secondary_Armor                := Sys_Round(100 * 10 / vehicle_tonnage)

      set bool_AI_Design_Add_Shields                       := bool_Enemy_Uses_Skip_Armor_Weapons and (vehicle_tonnage > 100)
      set bool_AI_Design_Add_Heavy_Weapon                  := TRUE
      set bool_AI_Design_Add_Primary_PD                    := (vehicle_tonnage > 100)
      set bool_AI_Design_Add_Supply_Storage                := bool_Enemy_Uses_Only_Engine_Weapons
      set bool_AI_Design_Add_Combat_Sensors                := (vehicle_tonnage > 100)
      set bool_AI_Design_Add_ECM                           := (vehicle_tonnage > 100)

      set lng_AI_Design_Max_Num_Heavy_Weapon               := 2
      set lng_AI_Design_Max_Num_Primary_PD                 := 0

    "Drone (Kamikaze)":
      set lng_AI_Design_Pct_Primary_Armor                  := 20

      set bool_AI_Design_Add_Combat_Thruster               := TRUE
      set bool_AI_Design_Add_No_Weapons                    := TRUE

  endcase

end

//------------------------------------------------------------------------
// Get_Weapon_Tonnage_To_Add
//------------------------------------------------------------------------
function Get_Weapon_Tonnage_To_Add returns boolean
params
  vehicle_type:              string
  vehicle_tonnage:           long
  ai_design_type:            string
vars
begin

  // Set our maximum space amounts
  if (vehicle_type = "Ship") then
    case vehicle_tonnage
      200:
        set lng_AI_Design_Weapon_Max_Tonnage               := 60
      225:
        set lng_AI_Design_Weapon_Max_Tonnage               := 60
      250:
        set lng_AI_Design_Weapon_Max_Tonnage               := 70
      300:
        set lng_AI_Design_Weapon_Max_Tonnage               := 90
      325:
        set lng_AI_Design_Weapon_Max_Tonnage               := 100
      350:
        set lng_AI_Design_Weapon_Max_Tonnage               := 100
      375:
        set lng_AI_Design_Weapon_Max_Tonnage               := 110
      400:
        set lng_AI_Design_Weapon_Max_Tonnage               := 120
      450:
        set lng_AI_Design_Weapon_Max_Tonnage               := 120
      480:
        set lng_AI_Design_Weapon_Max_Tonnage               := 140
      510:
        set lng_AI_Design_Weapon_Max_Tonnage               := 160
      540:
        set lng_AI_Design_Weapon_Max_Tonnage               := 180
      550:
        set lng_AI_Design_Weapon_Max_Tonnage               := 180
      570:
        set lng_AI_Design_Weapon_Max_Tonnage               := 180
      600:
        set lng_AI_Design_Weapon_Max_Tonnage               := 200
      750:
        set lng_AI_Design_Weapon_Max_Tonnage               := 250
      780:
        set lng_AI_Design_Weapon_Max_Tonnage               := 260
      810:
        set lng_AI_Design_Weapon_Max_Tonnage               := 270
      840:
        set lng_AI_Design_Weapon_Max_Tonnage               := 280
      870:
        set lng_AI_Design_Weapon_Max_Tonnage               := 290
      900:
        set lng_AI_Design_Weapon_Max_Tonnage               := 300
      1000:
        set lng_AI_Design_Weapon_Max_Tonnage               := 360
      1040:
        set lng_AI_Design_Weapon_Max_Tonnage               := 370
      1080:
        set lng_AI_Design_Weapon_Max_Tonnage               := 385
      1120:
        set lng_AI_Design_Weapon_Max_Tonnage               := 400
      1160:
        set lng_AI_Design_Weapon_Max_Tonnage               := 410
      1200:
        set lng_AI_Design_Weapon_Max_Tonnage               := 420
      1500:
        set lng_AI_Design_Weapon_Max_Tonnage               := 560
      1560:
        set lng_AI_Design_Weapon_Max_Tonnage               := 580
      1620:
        set lng_AI_Design_Weapon_Max_Tonnage               := 600
      1680:
        set lng_AI_Design_Weapon_Max_Tonnage               := 620
      1740:
        set lng_AI_Design_Weapon_Max_Tonnage               := 640
      1800:
        set lng_AI_Design_Weapon_Max_Tonnage               := 660
      2000:
        set lng_AI_Design_Weapon_Max_Tonnage               := 800
      2100:
        set lng_AI_Design_Weapon_Max_Tonnage               := 840
      2200:
        set lng_AI_Design_Weapon_Max_Tonnage               := 880
      2300:
        set lng_AI_Design_Weapon_Max_Tonnage               := 920
      2400:
        set lng_AI_Design_Weapon_Max_Tonnage               := 960
      2500:
        set lng_AI_Design_Weapon_Max_Tonnage               := 1000
    endcase
    // Backup for custom ship sizes
    if (lng_AI_Design_Weapon_Max_Tonnage = 0) then
      set lng_AI_Design_Weapon_Max_Tonnage := Sys_Trunc(vehicle_tonnage / 3.1)
    endif
  endif

  if (vehicle_type = "Base") then
    case vehicle_tonnage
      500:
        set lng_AI_Design_Weapon_Max_Tonnage               := 200
      600:
        set lng_AI_Design_Weapon_Max_Tonnage               := 250
      700:
        set lng_AI_Design_Weapon_Max_Tonnage               := 300
      800:
        set lng_AI_Design_Weapon_Max_Tonnage               := 350
      900:
        set lng_AI_Design_Weapon_Max_Tonnage               := 400
     1000:
        set lng_AI_Design_Weapon_Max_Tonnage               := 500
     2000:
        set lng_AI_Design_Weapon_Max_Tonnage               := 1000
     2200:
        set lng_AI_Design_Weapon_Max_Tonnage               := 1100
     2400:
        set lng_AI_Design_Weapon_Max_Tonnage               := 1200
     2600:
        set lng_AI_Design_Weapon_Max_Tonnage               := 1400
     2800:
        set lng_AI_Design_Weapon_Max_Tonnage               := 1600
     3000:
        set lng_AI_Design_Weapon_Max_Tonnage               := 1800
    endcase
    // Backup for custom sizes
    if (lng_AI_Design_Weapon_Max_Tonnage = 0) then
      set lng_AI_Design_Weapon_Max_Tonnage := Sys_Trunc(vehicle_tonnage / 1.8)
    endif
  endif
  // Troop
  if (vehicle_type = "Troop") then
    case vehicle_tonnage
      10:
        set lng_AI_Design_Weapon_Max_Tonnage               := 4
      20:
        set lng_AI_Design_Weapon_Max_Tonnage               := 9
      30:
        set lng_AI_Design_Weapon_Max_Tonnage               := 16
    endcase
    // Backup for custom sizes
    if (lng_AI_Design_Weapon_Max_Tonnage = 0) then
      set lng_AI_Design_Weapon_Max_Tonnage := Sys_Trunc(vehicle_tonnage / 2)
    endif
  endif
  // Fighter
  if (vehicle_type = "Fighter") then
    case vehicle_tonnage
      25:
        set lng_AI_Design_Weapon_Max_Tonnage               := 10
      50:
        set lng_AI_Design_Weapon_Max_Tonnage               := 20
    endcase
    // Backup for custom sizes
    if (lng_AI_Design_Weapon_Max_Tonnage = 0) then
      set lng_AI_Design_Weapon_Max_Tonnage := Sys_Trunc(vehicle_tonnage / 2.5)
    endif
  endif
  // Satellite
  if (vehicle_type = "Satellite") then
    case vehicle_tonnage
      100:
        set lng_AI_Design_Weapon_Max_Tonnage               := 60
      200:
        set lng_AI_Design_Weapon_Max_Tonnage               := 120
     endcase
    // Backup for custom sizes
    if (lng_AI_Design_Weapon_Max_Tonnage = 0) then
      set lng_AI_Design_Weapon_Max_Tonnage := Sys_Trunc(vehicle_tonnage / 1.66)
    endif
  endif
  // Weapon Platform
  if (vehicle_type = "Weapon Platform") then
    case vehicle_tonnage
      250:
        set lng_AI_Design_Weapon_Max_Tonnage               := 160
      500:
        set lng_AI_Design_Weapon_Max_Tonnage               := 320
    endcase
    // Backup for custom sizes
    if (lng_AI_Design_Weapon_Max_Tonnage = 0) then
      set lng_AI_Design_Weapon_Max_Tonnage := Sys_Trunc(vehicle_tonnage / 1.5)
    endif
  endif
  // Drone
  if (vehicle_type = "Drone") then
    case vehicle_tonnage
      100:
        set lng_AI_Design_Weapon_Max_Tonnage               := 40
      200:
        set lng_AI_Design_Weapon_Max_Tonnage               := 80
    endcase
    // Backup for custom sizes
    if (lng_AI_Design_Weapon_Max_Tonnage = 0) then
      set lng_AI_Design_Weapon_Max_Tonnage := Sys_Trunc(vehicle_tonnage / 2.5)
    endif
  endif

  // Adjustments
  // Religious races should use a bit less weapons on larger designs to accommodate specialty components
  if (bool_Race_Uses_Religious_Tech) then
    if (vehicle_tonnage > 600) then
      set lng_AI_Design_Weapon_Max_Tonnage := lng_AI_Design_Weapon_Max_Tonnage - 20
    endif
    if (vehicle_tonnage > 900) then
      set lng_AI_Design_Weapon_Max_Tonnage := lng_AI_Design_Weapon_Max_Tonnage - 30
    endif
    if (vehicle_tonnage > 1200) then
      set lng_AI_Design_Weapon_Max_Tonnage := lng_AI_Design_Weapon_Max_Tonnage - 40
    endif
  endif

  return TRUE
end

//------------------------------------------------------------------------
// Get_Extra_Components_To_Add
//------------------------------------------------------------------------
function Get_Extra_Components_To_Add returns boolean
params
  vehicle_type:              string
  vehicle_tonnage:           long
vars
  num_to_add:                long
begin

  // Clear previous components list
  call lst_AI_Extra_Comp_Name.clear()
  call lst_AI_Extra_Comp_Pct.clear()
  call lst_AI_Extra_Comp_Slot.clear()

  // Add Solar Collector or Solar Organelle (Organic)
  if (bool_AI_Design_Add_Solar_Collector) then
    if Is_Component_Available("Solar Organelle") then
      call lst_AI_Extra_Comp_Name.add("Solar Organelle")
    else
      if Is_Component_Available("Solar Collector") then
        call lst_AI_Extra_Comp_Name.add("Solar Collector")
      endif
    endif
    // How many?
    if (lng_AI_Design_Pct_Solar_Collector > 0) then
      call lst_AI_Extra_Comp_Pct.add(lng_AI_Design_Pct_Solar_Collector)
    else
      call lst_AI_Extra_Comp_Pct.add(-1)
    endif
    // Hull location?
    call lst_AI_Extra_Comp_Slot.add(SHIP_SECTION_OUTER_HULL)
  endif

  // Add Supply Storage or Supply Vat
  if (bool_AI_Design_Add_Supply_Storage) then
    // Troops or Fighters
    if (vehicle_type = "Troop") or (vehicle_type = "Fighter") then
      if Is_Component_Available("Small Supply Vat") then
        call lst_AI_Extra_Comp_Name.add("Small Supply Vat")
      else
        call lst_AI_Extra_Comp_Name.add("Small Supply Storage")
      endif
      // Add 1 to outer hull
      call lst_AI_Extra_Comp_Pct.add(-1)
      call lst_AI_Extra_Comp_Slot.add(SHIP_SECTION_OUTER_HULL)
    else
      // Other vehicles
      if Is_Component_Available("Supply Vat") then
        call lst_AI_Extra_Comp_Name.add("Supply Vat")
      else
        if (lng_AI_Design_Pct_Supply_Storage * vehicle_tonnage / 100 >= 20) then
          call lst_AI_Extra_Comp_Name.add("Large Supply Storage")
        else
          call lst_AI_Extra_Comp_Name.add("Supply Storage")
        endif
      endif
      // What percentage and hull location?
      call lst_AI_Extra_Comp_Pct.add(lng_AI_Design_Pct_Supply_Storage)
      call lst_AI_Extra_Comp_Slot.add(SHIP_SECTION_OUTER_HULL)
    endif
  endif

  // Add Ordnance Storage or Ordnance Vat (Organic)
  if (bool_AI_Design_Add_Ordnance_Storage) and (not bool_AI_Design_Skip_Ordnance_Storage) then
    // Troops or Fighters
    if (vehicle_type = "Troop") or (vehicle_type = "Fighter") then
      if Is_Component_Available("Small Ordnance Vat") then
        call lst_AI_Extra_Comp_Name.add("Small Ordnance Vat")
      else
        call lst_AI_Extra_Comp_Name.add("Small Ordnance Storage")
      endif
      // What percentage and hull location?
      call lst_AI_Extra_Comp_Pct.add(lng_AI_Design_Pct_Ordnance_Storage)
      call lst_AI_Extra_Comp_Slot.add(SHIP_SECTION_OUTER_HULL)
    else
      // Other vehicles
      if Is_Component_Available("Ordnance Vat") then
        call lst_AI_Extra_Comp_Name.add("Ordnance Vat")
      else
        if (lng_AI_Design_Pct_Ordnance_Storage * vehicle_tonnage / 100 >= 20) then
          call lst_AI_Extra_Comp_Name.add("Large Ordnance Storage")
        else
          call lst_AI_Extra_Comp_Name.add("Ordnance Storage")
        endif
      endif
      // What percentage and hull location?
      call lst_AI_Extra_Comp_Pct.add(lng_AI_Design_Pct_Ordnance_Storage)
      call lst_AI_Extra_Comp_Slot.add(SHIP_SECTION_OUTER_HULL)
    endif
  endif

 // Ordnance Replicator
  if Is_Component_Available("Ordnance Replicator") then
    call lst_AI_Extra_Comp_Name.add("Ordnance Replicator")
    call lst_AI_Extra_Comp_Pct.add(lng_AI_Design_Pct_Ordnance_Replicator)
    call lst_AI_Extra_Comp_Slot.add(SHIP_SECTION_OUTER_HULL)
  endif

  // Stealth Armor
  if (bool_AI_Design_Add_Stealth_Armor) then
    If Is_Component_Available("Stealth Armor") then
      call lst_AI_Extra_Comp_Name.add("Stealth Armor")
      call lst_AI_Extra_Comp_Pct.add(-1)
      call lst_AI_Extra_Comp_Slot.add(SHIP_SECTION_ARMOR)
    endif
  endif

  // Scattering Armor
  if (bool_AI_Design_Add_Scattering_Armor) then
    If Is_Component_Available("Scattering Armor") then
      call lst_AI_Extra_Comp_Name.add("Scattering Armor")
      call lst_AI_Extra_Comp_Pct.add(-1)
      call lst_AI_Extra_Comp_Slot.add(SHIP_SECTION_ARMOR)
    endif
  endif

  // Energy Refractor (Crystalline)
  if (bool_AI_Design_Add_Energy_Refractor) then
    If Is_Component_Available("Energy Refractor") then
      call lst_AI_Extra_Comp_Name.add("Energy Refractor")
      call lst_AI_Extra_Comp_Pct.add(-1)
      call lst_AI_Extra_Comp_Slot.add(SHIP_SECTION_OUTER_HULL)
    endif
  endif

  // Shields (Single Generator Addition Only)
  if (bool_AI_Design_Add_Shields) then
    if (vehicle_type = "Troop") or (vehicle_type = "Fighter") then
      // Small Null-Space Shields (Interdimensional)
      if Is_Component_Available("Small Null-Space Shield Generator") then
        call lst_AI_Extra_Comp_Name.add("Small Phased Shield Generator")
        call lst_AI_Extra_Comp_Pct.add(-1)
        call lst_AI_Extra_Comp_Slot.add(SHIP_SECTION_OUTER_HULL)
      else
        // Small Shield Organelle (Organic)
        if Is_Component_Available("Small Shield Organelle") then
          call lst_AI_Extra_Comp_Name.add("Small Shield Organelle")
          call lst_AI_Extra_Comp_Pct.add(-1)
          call lst_AI_Extra_Comp_Slot.add(SHIP_SECTION_OUTER_HULL)
        else
          // Phased Shields
          if Is_Component_Available("Small Phased Shield Generator") and (bool_Enemy_Uses_Skip_Shield_Weapons) then
            call lst_AI_Extra_Comp_Name.add("Small Phased Shield Generator")
            call lst_AI_Extra_Comp_Pct.add(-1)
            call lst_AI_Extra_Comp_Slot.add(SHIP_SECTION_OUTER_HULL)
          else
            // Normal Shields
            if Is_Component_Available("Small Shield Generator") then
              call lst_AI_Extra_Comp_Name.add("Small Shield Generator")
              call lst_AI_Extra_Comp_Pct.add(-1)
              call lst_AI_Extra_Comp_Slot.add(SHIP_SECTION_OUTER_HULL)
            endif
          endif
        endif
      endif
    else
      // Null-Space Shields (Interdimensional)
      if Is_Component_Available("Null-Space Shield Generator") then
        call lst_AI_Extra_Comp_Name.add("Null-Space Shield Generator")
        call lst_AI_Extra_Comp_Pct.add(-1)
        call lst_AI_Extra_Comp_Slot.add(SHIP_SECTION_OUTER_HULL)
      else
        // Shield Organelle (Organic)
        if Is_Component_Available("Shield Organelle") then
          call lst_AI_Extra_Comp_Name.add("Shield Organelle")
          call lst_AI_Extra_Comp_Pct.add(-1)
          call lst_AI_Extra_Comp_Slot.add(SHIP_SECTION_OUTER_HULL)
        else
          // Phased Shields
          if Is_Component_Available("Phased Shield Generator") and (bool_Enemy_Uses_Skip_Shield_Weapons) then
            call lst_AI_Extra_Comp_Name.add("Phased Shield Generator")
            call lst_AI_Extra_Comp_Pct.add(-1)
            call lst_AI_Extra_Comp_Slot.add(SHIP_SECTION_OUTER_HULL)
          else
            // Normal Shields
            call lst_AI_Extra_Comp_Name.add("Shield Generator")
            call lst_AI_Extra_Comp_Pct.add(-1)
            call lst_AI_Extra_Comp_Slot.add(SHIP_SECTION_OUTER_HULL)
          endif
        endif
      endif
    endif
  endif

  // Deflector Array
  if Is_Component_Available("Deflector Array") and (bool_AI_Design_Add_Deflector_Array) then
    call lst_AI_Extra_Comp_Name.add("Deflector Array")
    call lst_AI_Extra_Comp_Pct.add(-1)
    call lst_AI_Extra_Comp_Slot.add(SHIP_SECTION_OUTER_HULL)
  endif

  // Combat Sensors
  if (bool_AI_Design_Add_Combat_Sensors) then
    // Troop or Fighter
    if (vehicle_type = "Troop") or (vehicle_type = "Fighter") then
      call lst_AI_Extra_Comp_Name.add("Small Combat Sensors")
      call lst_AI_Extra_Comp_Pct.add(-1)
      call lst_AI_Extra_Comp_Slot.add(SHIP_SECTION_INNER_HULL)
    else
      // Other vehicles
      // Superluminal Sensors (Interdimensional)
      if Is_Component_Available("Superluminal Sensors") then
        call lst_AI_Extra_Comp_Name.add("Superluminal Sensors")
      else
        call lst_AI_Extra_Comp_Name.add("Combat Sensors")
      endif
      // Add 1 to inner hull
      call lst_AI_Extra_Comp_Pct.add(-1)
      call lst_AI_Extra_Comp_Slot.add(SHIP_SECTION_INNER_HULL)
    endif
  endif

  // ECM
  if (bool_AI_Design_Add_ECM) then
    if (vehicle_type = "Troop") or (vehicle_type = "Fighter") then
      // Troop or Fighter
      call lst_AI_Extra_Comp_Name.add("Small ECM")
      call lst_AI_Extra_Comp_Pct.add(-1)
      call lst_AI_Extra_Comp_Slot.add(SHIP_SECTION_INNER_HULL)
    else
      // Other vehicles
      call lst_AI_Extra_Comp_Name.add("ECM")
      call lst_AI_Extra_Comp_Pct.add(-1)
      call lst_AI_Extra_Comp_Slot.add(SHIP_SECTION_INNER_HULL)
    endif
  endif

  // Quantum Reactor or Flux Pod (Interdimensional)
  if (bool_AI_Design_Add_Supply_Generation) then
    if Is_Component_Available("Flux Pod") then
      call lst_AI_Extra_Comp_Name.add("Flux Pod")
      call lst_AI_Extra_Comp_Slot.add(SHIP_SECTION_INNER_HULL)
    else
      if Is_Component_Available("Solar Organelle") then
        call lst_AI_Extra_Comp_Name.add("Solar Organelle")
        call lst_AI_Extra_Comp_Slot.add(SHIP_SECTION_OUTER_HULL)
      else
        if Is_Component_Available("Quantum Reactor") then
          call lst_AI_Extra_Comp_Name.add("Quantum Reactor")
          call lst_AI_Extra_Comp_Slot.add(SHIP_SECTION_INNER_HULL)
        endif
      endif
    endif
    // How many to add?
    if (lng_AI_Design_Pct_Supply_Generation > 0) then
      call lst_AI_Extra_Comp_Pct.add(lng_AI_Design_Pct_Supply_Generation)
    else
      call lst_AI_Extra_Comp_Pct.add(-1)
    endif
  endif

  // Cloaking Device
  if (not bool_Are_We_Computer_Player) then
    if Is_Component_Available("Cloaking Device") and (bool_AI_Design_Add_Cloaking_Device) then
      call lst_AI_Extra_Comp_Name.add("Cloaking Device")
      call lst_AI_Extra_Comp_Pct.add(-1)
      call lst_AI_Extra_Comp_Slot.add(SHIP_SECTION_OUTER_HULL)
    endif
  endif

  // Advanced Sensors
  if (bool_AI_Design_Add_Sensors) then
    if Is_Component_Available("Psychic Receptors") then
      call lst_AI_Extra_Comp_Name.add("Psychic Receptors")
      call lst_AI_Extra_Comp_Pct.add(-1)
      call lst_AI_Extra_Comp_Slot.add(SHIP_SECTION_INNER_HULL)
      set bool_AI_Design_Skip_Basic_Sensors := TRUE
    else
      if Is_Component_Available("Temporal Sensors") then
        call lst_AI_Extra_Comp_Name.add("Temporal Sensors")
        call lst_AI_Extra_Comp_Pct.add(-1)
        call lst_AI_Extra_Comp_Slot.add(SHIP_SECTION_INNER_HULL)
        set bool_AI_Design_Skip_Basic_Sensors := TRUE
      else
        if Is_Component_Available("Tachyon Sensors") then
          call lst_AI_Extra_Comp_Name.add("Tachyon Sensors")
          call lst_AI_Extra_Comp_Pct.add(-1)
          call lst_AI_Extra_Comp_Slot.add(SHIP_SECTION_INNER_HULL)
          set bool_AI_Design_Skip_Basic_Sensors := TRUE
        endif
      endif
    endif
  endif

  // Shield Regenerator (Organic, Crystalline races shouldn't use)
  if Is_Component_Available("Shield Regenerator") and (bool_AI_Design_Add_Shield_Regenerator) and (not bool_Race_Uses_Organic_Tech) (not bool_Race_Uses_Crystalline_Tech) then
    call lst_AI_Extra_Comp_Name.add("Shield Regenerator")
    call lst_AI_Extra_Comp_Pct.add(-1 - Sys_Divide_Long(vehicle_tonnage, 500))
    call lst_AI_Extra_Comp_Slot.add(SHIP_SECTION_OUTER_HULL)
  endif

  // Security Station
  if Is_Component_Available("Security Station") and (bool_AI_Design_Add_Security_Station) then
    call lst_AI_Extra_Comp_Name.add("Security Station")
    call lst_AI_Extra_Comp_Pct.add(-1 - Sys_Trunc(vehicle_tonnage / 1000))
    call lst_AI_Extra_Comp_Slot.add(SHIP_SECTION_INNER_HULL)
  endif

  // Psychic Node or Neural Combat Net
  if (bool_Race_Uses_Combat_Best_Experience) then
    // Psychic Node (Psychic)
    if Is_Component_Available("Psychic Node") then
      call lst_AI_Extra_Comp_Name.add("Psychic Node")
      call lst_AI_Extra_Comp_Pct.add(-1)
      call lst_AI_Extra_Comp_Slot.add(SHIP_SECTION_INNER_HULL)
    else
      // Neutral Combat Net (Ruins)
      if Is_Component_Available("Neural Combat Net") then
        call lst_AI_Extra_Comp_Name.add("Neural Combat Net")
        call lst_AI_Extra_Comp_Pct.add(-1)
        call lst_AI_Extra_Comp_Slot.add(SHIP_SECTION_INNER_HULL)
      endif
    endif
  endif

  // Self-Destruct Device
  if Is_Component_Available("Self-Destruct Device") and (bool_AI_Design_Add_Self_Destruct) then
    call lst_AI_Extra_Comp_Name.add("Self-Destruct Device")
    call lst_AI_Extra_Comp_Pct.add(-1)
    call lst_AI_Extra_Comp_Slot.add(SHIP_SECTION_INNER_HULL)
  endif

  // Religious Components
  if Is_Component_Available("Religious Talisman") and (bool_AI_Design_Add_Talisman) then
    call lst_AI_Extra_Comp_Name.add("Religious Talisman")
    call lst_AI_Extra_Comp_Pct.add(-1)
    call lst_AI_Extra_Comp_Slot.add(SHIP_SECTION_INNER_HULL)
  endif

  if Is_Component_Available("Vengeance Totem") and (bool_AI_Design_Add_Vengeance_Totem) then
    call lst_AI_Extra_Comp_Name.add("Vengeance Totem")
    call lst_AI_Extra_Comp_Pct.add(-1)
    call lst_AI_Extra_Comp_Slot.add(SHIP_SECTION_INNER_HULL)
  endif

  if Is_Component_Available("Holy War Totem") and (bool_AI_Design_Add_Holy_War_Totem) then
    call lst_AI_Extra_Comp_Name.add("Holy War Totem")
    call lst_AI_Extra_Comp_Pct.add(-1)
    call lst_AI_Extra_Comp_Slot.add(SHIP_SECTION_INNER_HULL)
  endif

  if Is_Component_Available("Stealth Totem") and (bool_AI_Design_Add_Stealth_Totem) then
    call lst_AI_Extra_Comp_Name.add("Stealth Totem")
    call lst_AI_Extra_Comp_Pct.add(-1)
    call lst_AI_Extra_Comp_Slot.add(SHIP_SECTION_INNER_HULL)
  endif

  // Extra Movement
  if (bool_AI_Design_Add_Extra_Movement) then
    // Subspace Field Emitter (Interdimensional)
    if Is_Component_Available("Subspace Field Emitter") then
      call lst_AI_Extra_Comp_Name.add("Subspace Field Emitter")
      call lst_AI_Extra_Comp_Pct.add(-1)
      call lst_AI_Extra_Comp_Slot.add(SHIP_SECTION_OUTER_HULL)
    else
      // Solar Sail
      if Is_Component_Available("Solar Sail") then
        call lst_AI_Extra_Comp_Name.add("Solar Sail")
        call lst_AI_Extra_Comp_Pct.add(-1)
        call lst_AI_Extra_Comp_Slot.add(SHIP_SECTION_OUTER_HULL)
      endif
    endif
  endif

  // Inertial Dampeners (Combat Movement for Ships)
  if Is_Component_Available("Inertial Dampeners") and (bool_ai_design_add_combat_movement) then
    call lst_AI_Extra_Comp_Name.add("Inertial Dampeners")
    call lst_AI_Extra_Comp_Pct.add(-1)
    call lst_AI_Extra_Comp_Slot.add(SHIP_SECTION_OUTER_HULL)
  endif

  // Repair Bay
  if Is_Component_Available("Repair Bay") and (bool_AI_Design_Add_Repair_Bay) then
    call lst_AI_Extra_Comp_Name.add("Repair Bay")
    // How many to add?
    if (lng_AI_Design_Pct_Repair_Bay > 0) then
      call lst_AI_Extra_Comp_Pct.add(lng_AI_Design_Pct_Repair_Bay)
    else
      call lst_AI_Extra_Comp_Pct.add(-1)
    endif
    // Add to outer hull
    call lst_AI_Extra_Comp_Slot.add(SHIP_SECTION_OUTER_HULL)
  endif

  // Cargo Bay
  if Is_Component_Available("Cargo Bay") and (lng_AI_Design_Pct_Cargo_Bay > 0) then
    call lst_AI_Extra_Comp_Name.add("Cargo Bay")
    call lst_AI_Extra_Comp_Pct.add(lng_AI_Design_Pct_Cargo_Bay)
    call lst_AI_Extra_Comp_Slot.add(SHIP_SECTION_OUTER_HULL)
  endif

  // Medical Bay
  if Is_Component_Available("Medical Bay") and (bool_AI_Design_Add_Medical_Bay) then
    call lst_AI_Extra_Comp_Name.add("Medical Bay")
    call lst_AI_Extra_Comp_Pct.add(-1)
    call lst_AI_Extra_Comp_Slot.add(SHIP_SECTION_OUTER_HULL)
  endif

  // Long Range Scanners
  if Is_Component_Available("Long Range Scanners") and (bool_AI_Design_Add_Long_Range_Scanners) then
    call lst_AI_Extra_Comp_Name.add("Long Range Scanners")
    call lst_AI_Extra_Comp_Pct.add(-1)
    call lst_AI_Extra_Comp_Slot.add(SHIP_SECTION_OUTER_HULL)
  endif

  // Scanner Jammer
  if Is_Component_Available("Scanner Jammer") and (bool_AI_Design_Add_Scanner_Jammer) then
    call lst_AI_Extra_Comp_Name.add("Scanner Jammer")
    call lst_AI_Extra_Comp_Pct.add(-1)
    call lst_AI_Extra_Comp_Slot.add(SHIP_SECTION_OUTER_HULL)
  endif

  // Turbocharger (Troops)
  if Is_Component_Available("Turbocharger") and (bool_AI_Design_Add_Turbocharger) then
    call lst_AI_Extra_Comp_Name.add("Turbocharger")
    call lst_AI_Extra_Comp_Pct.add(-1)
    call lst_AI_Extra_Comp_Slot.add(SHIP_SECTION_OUTER_HULL)
  endif

  // Afterburners (Fighters)
  if Is_Component_Available("Afterburners") and (bool_AI_Design_Add_Afterburners) then
    call lst_AI_Extra_Comp_Name.add("Afterburners")
    call lst_AI_Extra_Comp_Pct.add(-1)
    call lst_AI_Extra_Comp_Slot.add(SHIP_SECTION_OUTER_HULL)
  endif

  // Combat Thruster (Drones)
  if Is_Component_Available("Combat Thruster") and (bool_AI_Design_Add_Combat_Thruster) then
    call lst_AI_Extra_Comp_Name.add("Combat Thruster")
    call lst_AI_Extra_Comp_Pct.add(-1)
    call lst_AI_Extra_Comp_Slot.add(SHIP_SECTION_OUTER_HULL)
  endif

  return TRUE
end

//------------------------------------------------------------------------
// Add_Components_To_Design
//------------------------------------------------------------------------
function Add_Components_To_Design returns boolean
params
  design_id:                 long
  ai_design_type:            string
vars
  continue_design:           boolean := TRUE
  vehicle_tonnage:           long
  vehicle_type:              string
  vehicle_design_type:       string
  comp_id:                   long
  comp_name:                 string
  comp_enh_id:               long := 0
  num_to_add:                long := 0
  num_primary_weapon:        long := 0
  num_secondary_weapon:      long := 0
  num_heavy_weapon:          long := 0
  num_special_weapon:        long := 0
  num_primary_pd:            long := 0
  num_secondary_pd:          long := 0
  weap_tonnage_left:         long := 0
  slot_to_add:               long
  size_id:                   long
  comp_tonnage:              long
  list_count:                long
  index:                     long
  add_weapons:               boolean := FALSE
  weapon_added:              boolean := FALSE
  extra_weap_space:          long
  pct_of_space:              long
begin

  // Get our design's details
  set size_id := Sys_Get_Vehicle_Design_Size_ID(design_id)
  set vehicle_tonnage := Sys_Get_Vehicle_Size_Tonnage_Space(sys_long_Player_ID, size_id)
  set vehicle_type := Sys_Get_Vehicle_Size_Vehicle_Type(size_id)
  set vehicle_design_type := Sys_Get_Vehicle_Design_AI_Design_Type(design_id)
  set extra_weap_space := Sys_Trunc(vehicle_tonnage * 0.02)
  set ai_design_type := Get_General_AI_Design_Type(ai_design_type)

  // Get our design's component percentages
  call Get_Component_Percentage_To_Add(ai_design_type, vehicle_tonnage)
  call Set_Race_Weapon_Choices(vehicle_type, vehicle_tonnage, ai_design_type)

  // Does our design use weapons?
  set add_weapons := (bool_AI_Design_Add_Primary_Weapon) or (bool_AI_Design_Add_Secondary_Weapon) or (bool_AI_Design_Add_Heavy_Weapon) or (bool_AI_Design_Add_Special_Weapon) or (bool_AI_Design_Add_Primary_PD) or (bool_AI_Design_Add_Secondary_PD) or (bool_AI_Design_Add_Special_PD)

  // Add Shields and Armor to our design
  if (continue_design) then

    // Add Shields to our design
    set comp_id := Get_Best_Component_Of_Type("Shields")

    if (comp_id > 0) then
      set comp_enh_id := Sys_Get_Best_Comp_Enh_For_Component(sys_long_Player_ID, design_id, comp_id)
      set comp_tonnage := Sys_Get_Component_Tonnage_Space(sys_long_Player_ID, comp_id, comp_enh_id)
      // Crystalline races use specific amounts of shield generators
      if (bool_Race_Uses_Crystalline_Tech) then
        if (vehicle_tonnage > 250) and (lng_AI_Design_Pct_Shields > 0) then
          set num_to_add := Sys_Min_Long(3, 1 + Sys_Trunc(vehicle_tonnage / 750))
        else
          set num_to_add := 0
        endif
      else
        set num_to_add := Sys_Trunc((vehicle_tonnage * (lng_AI_Design_Pct_Shields / 100)) / comp_tonnage)
      endif
      call Add_Components_To_Vehicle_Design(design_id, comp_id, comp_enh_id, SHIP_SECTION_OUTER_HULL, num_to_add, SPREAD_COMPONENTS_FROM_TOP)
    endif

    // Add Primary Armor
    set comp_id := Get_Best_Component_Of_Type("Primary Armor")

    if (comp_id > 0) then
      set comp_enh_id := Sys_Get_Best_Comp_Enh_For_Component(sys_long_Player_ID, design_id, comp_id)
      set comp_tonnage := Sys_Get_Component_Tonnage_Space(sys_long_Player_ID, comp_id, comp_enh_id)
      set num_to_add := Sys_Trunc((vehicle_tonnage * (lng_AI_Design_Pct_Primary_Armor / 100)) / comp_tonnage)
      call Add_Components_To_Vehicle_Design(design_id, comp_id, comp_enh_id, SHIP_SECTION_ARMOR, num_to_add, SPREAD_COMPONENTS_FROM_TOP)
    endif

    // Add Secondary Armor
    set comp_id := Get_Best_Component_Of_Type("Secondary Armor")

    if (comp_id > 0) then
      set comp_enh_id := Sys_Get_Best_Comp_Enh_For_Component(sys_long_Player_ID, design_id, comp_id)
      set comp_tonnage := Sys_Get_Component_Tonnage_Space(sys_long_Player_ID, comp_id, comp_enh_id)
      set num_to_add := Sys_Trunc((vehicle_tonnage * (lng_AI_Design_Pct_Secondary_Armor / 100)) / comp_tonnage)
      // Only Add 1 Emissive Armor for units (Temporary Restriction)
      if (vehicle_type <> "Ship") and (vehicle_type <> "Base") then
        if (comp_id = Sys_Get_Component_With_Name(sys_long_Player_ID, "Emissive Armor")) or (comp_id = Sys_Get_Component_With_Name(sys_long_Player_ID, "Small Emissive Armor")) then
          set num_to_add := 1
        endif
      endif
      call Add_Components_To_Vehicle_Design(design_id, comp_id, comp_enh_id, SHIP_SECTION_ARMOR, num_to_add, SPREAD_COMPONENTS_FROM_TOP)
    endif

    // Add weapons to our design if applicable
    if (add_weapons) then
      call Get_Weapon_Tonnage_To_Add(vehicle_type, vehicle_tonnage, ai_design_type)

      // Set the amount of weapons to add
      set weap_tonnage_left := lng_AI_Design_Weapon_Max_Tonnage

      loop
        set weapon_added := FALSE

        // Add Primary Weapon
        if (bool_AI_Design_Add_Primary_Weapon) and (num_primary_weapon < lng_AI_Design_Max_Num_Primary_Weapon) then
          set comp_id := Get_Best_Component_Of_Type("Primary Weapon")

          if (comp_id > 0) then
            set comp_enh_id := Sys_Get_Best_Comp_Enh_For_Component(sys_long_Player_ID, design_id, comp_id)
            set comp_tonnage := Sys_Get_Component_Tonnage_Space(sys_long_Player_ID, comp_id, comp_enh_id)

            if (comp_tonnage <= weap_tonnage_left) or (comp_tonnage - weap_tonnage_left <= extra_weap_space) then
              call Add_Components_To_Vehicle_Design(design_id, comp_id, comp_enh_id, SHIP_SECTION_OUTER_HULL, 1, SPREAD_COMPONENTS_FROM_TOP)
              set weap_tonnage_left := weap_tonnage_left - comp_tonnage
              set num_primary_weapon := num_primary_weapon + 1
              set weapon_added := TRUE
            endif
          endif
        endif

        // Add Secondary Weapon
        if (bool_AI_Design_Add_Secondary_Weapon) and (num_secondary_weapon < lng_AI_Design_Max_Num_Secondary_Weapon) then
          set comp_id := Get_Best_Component_Of_Type("Secondary Weapon")

          if (comp_id > 0) then
            set comp_enh_id := Sys_Get_Best_Comp_Enh_For_Component(sys_long_Player_ID, design_id, comp_id)
            set comp_tonnage := Sys_Get_Component_Tonnage_Space(sys_long_Player_ID, comp_id, comp_enh_id)

            if (comp_tonnage <= weap_tonnage_left) or (comp_tonnage - weap_tonnage_left <= extra_weap_space) then
              call Add_Components_To_Vehicle_Design(design_id, comp_id, comp_enh_id, SHIP_SECTION_OUTER_HULL, 1, SPREAD_COMPONENTS_FROM_TOP)
              set weap_tonnage_left := weap_tonnage_left - comp_tonnage
              set num_secondary_weapon := num_secondary_weapon + 1
              set weapon_added := TRUE
            endif
          endif
        endif

        // Add Heavy Weapon
        if (bool_AI_Design_Add_Heavy_Weapon) and (num_heavy_weapon < lng_AI_Design_Max_Num_Heavy_Weapon) then
          set comp_id := Get_Best_Component_Of_Type("Heavy Weapon")

          if (comp_id > 0) then
            set comp_enh_id := Sys_Get_Best_Comp_Enh_For_Component(sys_long_Player_ID, design_id, comp_id)
            set comp_tonnage := Sys_Get_Component_Tonnage_Space(sys_long_Player_ID, comp_id, comp_enh_id)

            if (comp_tonnage <= weap_tonnage_left) or (comp_tonnage - weap_tonnage_left <= extra_weap_space) then
              call Add_Components_To_Vehicle_Design(design_id, comp_id, comp_enh_id, SHIP_SECTION_OUTER_HULL, 1, SPREAD_COMPONENTS_FROM_TOP)
              set weap_tonnage_left := weap_tonnage_left - comp_tonnage
              set num_heavy_weapon := num_heavy_weapon + 1
              set weapon_added := TRUE
            endif
          endif
        endif

        // Add Special Weapon
        if (bool_AI_Design_Add_Special_Weapon) and (num_special_weapon < lng_AI_Design_Max_Num_Special_Weapon) then
          set comp_id := Get_Best_Component_Of_Type("Special Weapon")

          if (comp_id > 0) then
            set comp_enh_id := Sys_Get_Best_Comp_Enh_For_Component(sys_long_Player_ID, design_id, comp_id)
            // Don't use Allegiance Subverter with mounts
            If (comp_id = Sys_Get_Component_With_Name(sys_long_Player_ID, "Allegiance Subverter")) then
              set comp_enh_id := 0
            endif
            set comp_tonnage := Sys_Get_Component_Tonnage_Space(sys_long_Player_ID, comp_id, comp_enh_id)

            if (comp_tonnage <= weap_tonnage_left) or (comp_tonnage - weap_tonnage_left <= extra_weap_space) then
              call Add_Components_To_Vehicle_Design(design_id, comp_id, comp_enh_id, SHIP_SECTION_OUTER_HULL, 1, SPREAD_COMPONENTS_FROM_TOP)
              set weap_tonnage_left := weap_tonnage_left - comp_tonnage
              set num_special_weapon := num_special_weapon + 1
              set weapon_added := TRUE
            endif
          endif
        endif

        // Add Primary Point Defense
        if (bool_AI_Design_Add_Primary_PD) and (num_primary_pd < lng_AI_Design_Max_Num_Primary_PD) then
          set comp_id := Get_Best_Component_Of_Type("Primary Point-Defense Weapon")

          if (comp_id > 0) then
            set comp_enh_id := Sys_Get_Best_Comp_Enh_For_Component(sys_long_Player_ID, design_id, comp_id)
            set comp_tonnage := Sys_Get_Component_Tonnage_Space(sys_long_Player_ID, comp_id, comp_enh_id)

            if (comp_tonnage <= weap_tonnage_left) or (comp_tonnage - weap_tonnage_left <= extra_weap_space) then
              call Add_Components_To_Vehicle_Design(design_id, comp_id, comp_enh_id, SHIP_SECTION_OUTER_HULL, 1, SPREAD_COMPONENTS_FROM_TOP)
              set weap_tonnage_left := weap_tonnage_left - comp_tonnage
              set num_primary_pd := num_primary_pd + 1
              set weapon_added := TRUE
            endif
          endif
        endif

        // Add Secondary Point Defense
        if (bool_AI_Design_Add_Secondary_PD) and (num_secondary_pd < lng_AI_Design_Max_Num_Secondary_PD) then
          set comp_id := Get_Best_Component_Of_Type("Secondary Point-Defense Weapon")

          if (comp_id > 0) then
            set comp_enh_id := Sys_Get_Best_Comp_Enh_For_Component(sys_long_Player_ID, design_id, comp_id)
            set comp_tonnage := Sys_Get_Component_Tonnage_Space(sys_long_Player_ID, comp_id, comp_enh_id)

            if (comp_tonnage <= weap_tonnage_left) or (comp_tonnage - weap_tonnage_left <= extra_weap_space) then
              call Add_Components_To_Vehicle_Design(design_id, comp_id, comp_enh_id, SHIP_SECTION_OUTER_HULL, 1, SPREAD_COMPONENTS_FROM_TOP)
              set weap_tonnage_left := weap_tonnage_left - comp_tonnage
              set num_secondary_pd := num_secondary_pd + 1
              set weapon_added := TRUE
            endif
          endif
        endif

        // Add Special Point Defense
        if (bool_AI_Design_Add_Special_PD) and (num_secondary_pd < lng_AI_Design_Max_Num_Secondary_PD) then
          set comp_id := Get_Best_Component_Of_Type("Secondary Point-Defense Weapon")

          if (comp_id > 0) then
            set comp_enh_id := Sys_Get_Best_Comp_Enh_For_Component(sys_long_Player_ID, design_id, comp_id)
            set comp_tonnage := Sys_Get_Component_Tonnage_Space(sys_long_Player_ID, comp_id, comp_enh_id)

            if (comp_tonnage <= weap_tonnage_left) or (comp_tonnage - weap_tonnage_left <= extra_weap_space) then
              call Add_Components_To_Vehicle_Design(design_id, comp_id, comp_enh_id, SHIP_SECTION_OUTER_HULL, 1, SPREAD_COMPONENTS_FROM_TOP)
              set weap_tonnage_left := weap_tonnage_left - comp_tonnage
              set num_secondary_pd := num_secondary_pd + 1
              set weapon_added := TRUE
            endif
          endif
        endif
        exitwhen (weap_tonnage_left <= 0) or (not weapon_added)
      endloop
      // Don't create design if no weapons were added
      if (weap_tonnage_left = lng_AI_Design_Weapon_Max_Tonnage) then
        set continue_design := FALSE
      endif
    endif

    // Does our design require ordnance?
    if (Sys_Get_Vehicle_Design_Total_Ordnance_Usage(design_id, sys_long_Player_ID) > 0) then
      set bool_AI_Design_Add_Ordnance_Storage := TRUE
      // Set the amount of ordnance to add
      if (vehicle_type = "Troop") or (vehicle_type = "Fighter") then
        set lng_AI_Design_Pct_Ordnance_Storage := -1
      else
        set lng_AI_Design_Pct_Ordnance_Storage := 2
      endif
    endif

    // Get a list of extra components to add
    call Get_Extra_Components_To_Add(vehicle_type, vehicle_tonnage)

    if (continue_design) and (Sys_Get_Vehicle_Design_Remaining_Tonnage_Space(design_id) > 0) then
      set list_count := lst_AI_Extra_Comp_Name.count()
    
      if (list_count > 0) then
        for index := 1 to list_count do
          set comp_id := Sys_Get_Component_With_Name(sys_long_Player_ID, lst_AI_Extra_Comp_Name.get(index))
          set comp_name := lst_AI_Extra_Comp_Name.get(index)
          
          if (Sys_Is_Component_Available(sys_long_Player_ID, comp_id)) then
            set comp_enh_id := Sys_Get_Best_Comp_Enh_For_Component(sys_long_Player_ID, design_id, comp_id)
            set comp_tonnage := Sys_Get_Component_Tonnage_Space(sys_long_Player_ID, comp_id, comp_enh_id)
            set pct_of_space := lst_AI_Extra_Comp_Pct.get(index)
            set slot_to_add := lst_AI_Extra_Comp_Slot.get(index)
            set num_to_add := 0
            
            if (pct_of_space < 0) then
              set num_to_add := -pct_of_space
            else
              set num_to_add := Sys_Trunc((vehicle_tonnage * (pct_of_space / 100)) / comp_tonnage)
            endif
            // Add the component to our design
            if (num_to_add > 0) then
              call Add_Components_To_Vehicle_Design(design_id, comp_id, comp_enh_id, slot_to_add, num_to_add, SPREAD_COMPONENTS_FROM_TOP)
            endif
          endif
        endfor
      endif
    endif

    // Fill up any remaining space with armor
    if (continue_design) and (Sys_Get_Vehicle_Design_Remaining_Tonnage_Space(design_id) > 0) then
      if (bool_AI_Design_Add_Bulkheads) then
        if Is_Component_Available("Crystalline Bulkhead") then
          set comp_id := Sys_Get_Component_With_Name(sys_long_Player_ID, "Crystalline Bulkhead")
        else
          if Is_Component_Available("Organic Bulkhead") then
            set comp_id := Sys_Get_Component_With_Name(sys_long_Player_ID, "Organic Bulkhead")
          else
            if Is_Component_Available("Bulkhead") then
              set comp_id := Sys_Get_Component_With_Name(sys_long_Player_ID, "Bulkhead")
            endif
          endif
        endif
        // Add bulkheads
        call Add_Components_To_Vehicle_Design(design_id, comp_id, comp_enh_id, SHIP_SECTION_OUTER_HULL, 100, SPREAD_COMPONENTS_RANDOMLY)
      else
        set comp_id := Get_Best_Component_of_Type("Primary Armor")
        call Add_Components_To_Vehicle_Design(design_id, comp_id, comp_enh_id, SHIP_SECTION_ARMOR, 100, SPREAD_COMPONENTS_FROM_TOP)
      endif
    endif

    if (continue_design) and (Sys_Get_Vehicle_Design_Remaining_Tonnage_Space(design_id) > 0) then
      if Is_Component_Available("Light Organic Armor") then
        set comp_id := Sys_Get_Component_With_Name(sys_long_Player_ID, "Light Organic Armor")
      else
         if Is_Component_Available("Light Crystalline Armor") then
           set comp_id := Sys_Get_Component_With_Name(sys_long_Player_ID, "Light Crystalline Armor")
         else
           set comp_id := Sys_Get_Component_With_Name(sys_long_Player_ID, "Light Armor")
         endif
      endif
      call Add_Components_To_Vehicle_Design(design_id, comp_id, comp_enh_id, SHIP_SECTION_ARMOR, 100, SPREAD_COMPONENTS_FROM_TOP)
    endif
  endif

  return continue_design
end

//------------------------------------------------------------------------
// Add_Components_To_Vehicle_Design
//------------------------------------------------------------------------
function Add_Components_To_Vehicle_Design returns boolean
params
  design_id:                 long
  comp_id:                   long
  comp_enh_id:               long
  ship_section_id:           long
  num_comps_to_add:          long
  spread_direction:          long
vars
  current_tonnage:           long
  total_tonnage:             long
  comp_tonnage:              long
  index:                     long
  remaining_tonnage:         long
  comp_tonnage:              long
  comp_spread_type:          long
  comp_spread_id:            long
  retval:                    boolean
begin

  // Get the component's details
  set comp_tonnage := Sys_Get_Component_Tonnage_Space(sys_long_Player_ID, comp_id, comp_enh_id)
  set comp_spread_type := COMP_SPREAD_TYPE_DIRECTION
  // Default spreading is from the top
  set comp_spread_id := COMP_SPREAD_ID_DIR_TOP_DOWN

  // For some components spread from the bottom
  if (spread_direction = SPREAD_COMPONENTS_FROM_BOTTOM) then
    set comp_spread_id := COMP_SPREAD_ID_DIR_BOTTOM_UP
  endif

  if (num_comps_to_add > 0) then
    set index := 0
    loop
      set index := index + 1
      set remaining_tonnage := Sys_Get_Vehicle_Design_Remaining_Tonnage_Space(design_id)
      set retval := FALSE
      // For components with random spread...
      if (spread_direction = SPREAD_COMPONENTS_RANDOMLY) then
        if (Sys_Get_Random_Long(1, 2) = 1) then
          // From top...
          set comp_spread_id := COMP_SPREAD_ID_DIR_TOP_DOWN
        else
          // From bottom...
          set comp_spread_id := COMP_SPREAD_ID_DIR_BOTTOM_UP
        endif
      endif
      // Add the component if space is still available
      if (remaining_tonnage >= comp_tonnage) then
        set retval := Sys_Add_Component_To_Vehicle_Design(sys_long_Player_ID, design_id, comp_id, comp_enh_id, ship_section_id, comp_spread_type, comp_spread_id)
      endif
      exitwhen ((index >= num_comps_to_add) or (not retval))
    endloop
  endif

end

//------------------------------------------------------------------------
// Get_Best_Component_Of_Type
//------------------------------------------------------------------------
function Get_Best_Component_Of_Type returns long
params
  comp_type:                 string
vars
  comp_id:                   long
  final_comp_id:             long := 0
begin

  // Get the best component of the wanted type
  case (comp_type)
    "Engine":
      if Is_Component_Available("Quantum Engine") then
        set final_comp_id := Sys_Get_Component_With_Name(sys_long_Player_ID, "Quantum Engine")
      else
        if Is_Component_Available("Jacketed-Photon Engine") then
          set final_comp_id := Sys_Get_Component_With_Name(sys_long_Player_ID, "Jacketed-Photon Engine")
        else
          if Is_Component_Available("Contra-Terrene Engine") then
            set final_comp_id := Sys_Get_Component_With_Name(sys_long_Player_ID, "Contra-Terrene Engine")
          else
            if Is_Component_Available("Ion Engine") then
              set final_comp_id := Sys_Get_Component_With_Name(sys_long_Player_ID, "Ion Engine")
             endif
          endif
        endif
      endif
    "Small Engine":
      if Is_Component_Available("Small Quantum Engine") then
        set final_comp_id := Sys_Get_Component_With_Name(sys_long_Player_ID, "Small Quantum Engine")
      else
        if Is_Component_Available("Small Jacketed-Photon Engine") then
          set final_comp_id := Sys_Get_Component_With_Name(sys_long_Player_ID, "Small Jacketed-Photon Engine")
        else
          if Is_Component_Available("Small Contra-Terrene Engine") then
            set final_comp_id := Sys_Get_Component_With_Name(sys_long_Player_ID, "Small Contra-Terrene Engine")
          else
            if Is_Component_Available("Small Ion Engine") then
              set final_comp_id := Sys_Get_Component_With_Name(sys_long_Player_ID, "Small Ion Engine")
             endif
          endif
        endif
      endif
    "Ground Movement":
      if Is_Component_Available("Quantum Ground Thrusters") then
        set final_comp_id := Sys_Get_Component_With_Name(sys_long_Player_ID, "Quantum Ground Thrusters")
      else
        if Is_Component_Available("Anti-Grav Ground Thrusters") then
          set final_comp_id := Sys_Get_Component_With_Name(sys_long_Player_ID, "Anti-Grav Ground Thrusters")
        else
          if Is_Component_Available("Fusion Ground Thrusters") then
            set final_comp_id := Sys_Get_Component_With_Name(sys_long_Player_ID, "Fusion Ground Thrusters")
          else
            if Is_Component_Available("Nuclear Ground Thrusters") then
              set final_comp_id := Sys_Get_Component_With_Name(sys_long_Player_ID, "Nuclear Ground Thrusters")
             endif
          endif
        endif
      endif
    "Primary Weapon":
      set final_comp_id := Get_Wanted_Component_From_List(lst_Primary_Weapon_Name)
    "Secondary Weapon":
      set final_comp_id := Get_Wanted_Component_From_List(lst_Secondary_Weapon_Name)
    "Heavy Weapon":
      set final_comp_id := Get_Wanted_Component_From_List(lst_Heavy_Weapon_Name)
    "Special Weapon":
      set final_comp_id := Get_Wanted_Component_From_List(lst_Special_Weapon_Name)
    "Bombardment Weapon":
      set final_comp_id := Get_Wanted_Component_From_List(lst_Bombardment_Weapon_Name)
    "Primary Point-Defense Weapon":
      set final_comp_id := Get_Wanted_Component_From_List(lst_Primary_PD_Weapon_Name)
    "Secondary Point-Defense Weapon":
      set final_comp_id := Get_Wanted_Component_From_List(lst_Secondary_PD_Weapon_Name)
    "Special Point-Defense Weapon":
      set final_comp_id := Get_Wanted_Component_From_List(lst_Special_PD_Weapon_Name)
    "Shields":
      // Null-Space Shields (Interdimensional)
      if Is_Component_Available("Null-Space Shield Generator") then
        set final_comp_id := Sys_Get_Component_With_Name(sys_long_Player_ID, "Null-Space Shield Generator")
      else
        // Shield Organelle (Organic)
        if Is_Component_Available("Shield Organelle") then
          set final_comp_id := Sys_Get_Component_With_Name(sys_long_Player_ID, "Shield Organelle")
        else
          // Phased Shields
          if Is_Component_Available("Phased Shield Generator") and (bool_Enemy_Uses_Skip_Shield_Weapons) then
            set final_comp_id := Sys_Get_Component_With_Name(sys_long_Player_ID, "Phased Shield Generator")
          else
            // Normal Shields
            if Is_Component_Available("Shield Generator") then
              set final_comp_id := Sys_Get_Component_With_Name(sys_long_Player_ID, "Shield Generator")
            endif
          endif
        endif
      endif
    "Primary Armor":
      set final_comp_id := Get_Wanted_Component_From_List(lst_Primary_Armor_Name)
    "Secondary Armor":
      set final_comp_id := Get_Wanted_Component_From_List(lst_Secondary_Armor_Name)
  endcase

  return final_comp_id
end

//------------------------------------------------------------------------
// Get_Wanted_Component_From_List
//------------------------------------------------------------------------
function Get_Wanted_Component_From_List returns long
params
  lst_wanted_names:          stringlist
vars
  comp_id:                   long
  final_comp_id:             long := 0
  list_count:                long
  index:                     long
begin

  // Retrieve our list of wanted components
  set list_count := lst_wanted_names.count()

  if (list_count > 0) then
    for index := 1 to list_count do
      if (final_comp_id = 0) then
        set comp_id := Sys_Get_Component_With_Name(sys_long_Player_ID, lst_wanted_names.get(index))
        // Is the component available?
        if Sys_Is_Component_Available(sys_long_Player_ID, comp_id) then
          set final_comp_id := comp_id
        endif
      endif
    endfor
  endif

  return final_comp_id
end

//------------------------------------------------------------------------
// Pick_Vehicle_Size
//------------------------------------------------------------------------
function Pick_Vehicle_Size returns long
params
  ai_design_type:            string
  ai_design_type_index:      long
vars
  size_id:                   long
  ai_vehicle_size:           long
  vehicle_size_name:         string
  index:                     long
  num_items:                 long
begin
  return 0

  // Get the vehicle size to use for this design
  set ai_vehicle_size := lst_AI_Design_Type_Vehicle_Size.get(ai_design_type_index)

  set num_items := lst_AI_Vehicle_Sizes_Name.count()
  if (num_items > 0) then
    for index := 1 to num_items do
      if (lst_AI_Vehicle_Sizes_AI_Size.get(index) = ai_vehicle_size) then
        set vehicle_size_name := lst_AI_Vehicle_Sizes_Name.get(index)

        set size_id := Sys_Get_Vehicle_Size_From_Name(vehicle_size_name)
        if (size_id > 0) then
          if Sys_Is_Vehicle_Size_Available(sys_long_Player_ID, size_id) then
            return size_id
          endif
        endif

      endif
    endfor
  endif

end

//------------------------------------------------------------------------
// Get_Number_Of_Components_To_Add
//------------------------------------------------------------------------
function Get_Number_Of_Components_To_Add returns long
params
  design_id:                 long
  design_type:               string
  comp_type:                 string
vars
  ship_size_id:              long
  ship_size_name:            string
  index:                     long := 0
  num_controls:              long := 0
  num_engines:               long := 0
  num_crew:                  long := 0
  num_to_add:                long
  is_combat_ship:            boolean := FALSE
  tech_id:                   long
  tech_level:                long
begin 

  // Get our design's details
  set ship_size_id := Sys_Get_Vehicle_Design_Size_ID(design_id)
  set ship_size_name := Sys_Get_Vehicle_Size_Name(ship_size_id)
  set index := lst_AI_Vehicle_Sizes_Name.indexof(ship_size_name)
  
  // Mark basic combat designs
  if (design_type = "Ship (Attack)") or (design_type = "Ship (Defense)") or (design_type = "Ship (Seeker)") or (design_type = "Ship (Attack Base)") or (design_type = "Ship (Point-Defense)") or (design_type = "Ship (Patrol)") then
    set is_combat_ship := TRUE
  endif

  // Get the required components to add
  if (index > 0) then
    case lst_AI_Vehicle_Sizes_AI_Size.get(index)
      AI_VEHICLE_SIZE_FRIGATE:
        set num_engines := 4
        set num_crew := 100
        set num_controls := 1
      AI_VEHICLE_SIZE_DESTROYER:
        set num_engines := 6
        set num_crew := 150
        set num_controls := 1
      AI_VEHICLE_SIZE_LIGHT_CRUISER:
        set num_engines := 8
        set num_crew := 250
        set num_controls := 2
      AI_VEHICLE_SIZE_CRUISER:
        set num_engines := 12
        set num_crew := 400
        set num_controls := 2
      AI_VEHICLE_SIZE_BATTLESHIP:
        set num_engines := 16
        set num_crew := 500
        set num_controls := 3
      AI_VEHICLE_SIZE_DREADNOUGHT:
        set num_engines := 24
        set num_crew := 700
        set num_controls := 4
      AI_VEHICLE_SIZE_BASESHIP:
        set num_engines := 30
        set num_crew := 1000
        set num_controls := 5
      AI_VEHICLE_SIZE_LIGHT_CARRIER:
        set num_engines := 8
        set num_crew := 300
        set num_controls := 2
      AI_VEHICLE_SIZE_CARRIER:
        set num_engines := 16
        set num_crew := 500
        set num_controls := 3
      AI_VEHICLE_SIZE_HEAVY_CARRIER:
        set num_engines := 24
        set num_crew := 700
        set num_controls := 4
      AI_VEHICLE_SIZE_COLONY_SHIP:
        set num_engines := 6
        set num_crew := 100
        set num_controls := 1
      AI_VEHICLE_SIZE_SM_FREIGHTER:
        set num_engines := 4
        set num_crew := 100
        set num_controls := 1
      AI_VEHICLE_SIZE_MD_FREIGHTER:
        set num_engines := 12
        set num_crew := 200
        set num_controls := 1
      AI_VEHICLE_SIZE_LG_FREIGHTER:
        set num_engines := 20
        set num_crew := 300
        set num_controls := 2
      AI_VEHICLE_SIZE_SPACE_STATION:
        set num_crew := 300
        set num_controls := 2
      AI_VEHICLE_SIZE_STARBASE:
        set num_crew := 1000
        set num_controls := 5
      AI_VEHICLE_SIZE_SM_TROOP:
        set num_engines := 2
      AI_VEHICLE_SIZE_MD_TROOP:
        set num_engines := 4
      AI_VEHICLE_SIZE_LG_TROOP:
        set num_engines := 6
      AI_VEHICLE_SIZE_SM_FIGHTER:
        set num_engines := 2
      AI_VEHICLE_SIZE_LG_FIGHTER:
        set num_engines := 4
      AI_VEHICLE_SIZE_SM_DRONE:
        set num_engines := 1
     AI_VEHICLE_SIZE_LG_DRONE:
        set num_engines := 2
    endcase
  endif

  // Special case for Autonomic Ganglion usage
  if Is_Component_Available("Autonomic Ganglion") then
  // For races that use Autonomic Ganglions on all vehicles
    if (bool_Race_Uses_Autonomic_Ganglions_Always) or (bool_Race_Uses_Autonomic_Ganglions_Only_Combat and is_combat_ship) then
      // Adjust crew requirement
      set num_crew := 0
    else
      // For races that use Autonomic Ganglions only on combat ships
      if (bool_Race_Uses_Autonomic_Ganglions_Only_Combat) then
        case num_controls
          1:
            set num_controls := 1
          2:
            set num_controls := 1
          3:
            set num_controls := 1
          4:
            set num_controls := 2
          5:
            set num_controls := 2
        endcase
        // Adjust crew requirement
        set num_crew := Sys_Max_Long(0, num_crew - (num_controls * 200))
      endif
    endif
  endif

  // Set the number of engines, crew quarters, and life supports required for our design
  case (comp_type)
    "Autonomic Ganglion":
      return num_controls
    "Life Support":
      set tech_id := Sys_Get_Tech_ID("Vehicle Systems")
      set tech_level := Sys_Get_Empire_Research_Tech_Level(sys_long_Player_ID, tech_id)
      set num_to_add := Sys_Trunc(num_crew / (40 + tech_level * 10) + 0.99)
      return num_to_add
    "Crew Quarters":
      set tech_id := Sys_Get_Tech_ID("Vehicle Systems")
      set tech_level := Sys_Get_Empire_Research_Tech_Level(sys_long_Player_ID, tech_id)
      set num_to_add := Sys_Trunc(num_crew / (40 + tech_level * 10) + 0.99)
      return num_to_add
    "Engine":
      return num_engines
    "Small Engine":
      return num_engines
    "Ground Movement":
      return num_engines
  endcase

end

//------------------------------------------------------------------------
// Add_Required_Components_To_Design
//------------------------------------------------------------------------
function Add_Required_Components_To_Design returns boolean
params
  design_id:                 long
  ai_design_type:            string
vars
  bool_continue_design:      boolean := TRUE
  num_to_add:                long
  num_slots:                 long
  slot_mod:                  long
  num_launchers:             long
  num_cargo_bays:            long
  comp_id:                   long
  size_name:                 string
  size_id:                   long
  vehicle_tonnage:           long
  ai_vehicle_size:           long
  design_type:               string
  comp_tonnage:              long
  vehicle_type:              string
  comp_name:                 string
  pct_space:                 long
  comp_abil:                 string
  comp_enh_id:               long
  add_only_one:              boolean
  index:                     long := 0
  slot_loc:                  long
begin

  // Get our design's basic details
  set size_id := Sys_Get_Vehicle_Design_Size_ID(design_id)
  set vehicle_tonnage := Sys_Get_Vehicle_Size_Tonnage_Space(sys_long_Player_ID, size_id)
  set size_name := Sys_Get_Vehicle_Size_Name(size_id)
  set vehicle_type := Sys_Get_Vehicle_Size_Vehicle_Type(size_id)
  set ai_vehicle_size := Get_AI_Vehicle_Size(size_name)
  set bool_AI_Design_Add_No_Weapons := FALSE
  // Convert our design's AI design type to a general AI design type
  set design_type := Get_General_AI_Design_Type(ai_design_type)

  // 9. Add required Ship and Base Components
  if (Sys_Is_Vehicle_Size_Ship(size_id) or Sys_Is_Vehicle_Size_Base(size_id)) then
    if (bool_Race_Uses_Master_Computers) then
      // Temp bug fix
      case design_type
        "Base (Space Yard)":
          set bool_AI_Design_Add_No_Weapons := vehicle_tonnage < 800
        "Ship (Explorer)":
          set bool_AI_Design_Add_No_Weapons := TRUE
        "Ship (Repair)":
          set bool_AI_Design_Add_No_Weapons := TRUE
        "Ship (Space Yard)":
          set bool_AI_Design_Add_No_Weapons := TRUE
        "Ship (Medical)":
          set bool_AI_Design_Add_No_Weapons := TRUE
        "Ship (Mine Sweeper)":
          set bool_AI_Design_Add_No_Weapons := TRUE
        "Ship (Mine Layer)":
          set bool_AI_Design_Add_No_Weapons := TRUE
        "Ship (Remote Mining)":
          set bool_AI_Design_Add_No_Weapons := TRUE
        "Ship (Remote Farming)":
          set bool_AI_Design_Add_No_Weapons := TRUE
        "Ship (Remote Refining)":
          set bool_AI_Design_Add_No_Weapons := TRUE
        "Ship (Satellite Launcher)":
          set bool_AI_Design_Add_No_Weapons := TRUE
        "Carrier (Fighter)":
          set bool_AI_Design_Add_No_Weapons := TRUE
        "Carrier (Drone)":
          set bool_AI_Design_Add_No_Weapons := TRUE
        "Transport (Supply)":
          set bool_AI_Design_Add_No_Weapons := TRUE
        "Transport (Population)":
          set bool_AI_Design_Add_No_Weapons := TRUE
        "Transport (Cargo)":
          set bool_AI_Design_Add_No_Weapons := TRUE
        "Colonizer (Rock)":
          set bool_AI_Design_Add_No_Weapons := TRUE
        "Colonizer (Ice)":
          set bool_AI_Design_Add_No_Weapons := TRUE
        "Colonizer (Gas)":
          set bool_AI_Design_Add_No_Weapons := TRUE
      endcase
      // Add Control Computer for non-combat ships
      if (bool_AI_Design_Add_No_Weapons) then
        set comp_id := Sys_Get_Component_With_Name(sys_long_Player_ID, "Control Computer")
      else
        set comp_id := Sys_Get_Component_With_Name(sys_long_Player_ID, "Master Computer")
      endif
    endif
    // Add Control/Master Computer to design
    if (comp_id > 0) then
      set comp_enh_id := Sys_Get_Best_Comp_Enh_For_Component(sys_long_Player_ID, design_id, comp_id)
      call Add_Components_To_Vehicle_Design(design_id, comp_id, comp_enh_id, SHIP_SECTION_INNER_HULL, 1, SPREAD_COMPONENTS_FROM_TOP)
    else
      if Is_Component_Available("Autonomic Ganglion") and (bool_Race_Uses_Autonomic_Ganglions_Always or bool_Race_Uses_Autonomic_Ganglions_Only_Combat) then
        set comp_id := Sys_Get_Component_With_Name(sys_long_Player_ID, "Autonomic Ganglion")
        if (comp_id > 0) then
          set comp_enh_id := Sys_Get_Best_Comp_Enh_For_Component(sys_long_Player_ID, design_id, comp_id)
          set num_to_add := Get_Number_Of_Components_To_Add(design_id, design_type, "Autonomic Ganglion")
          if (num_to_add > 0) then
            // Add Autonomic Ganglions to our design
            call Add_Components_To_Vehicle_Design(design_id, comp_id, comp_enh_id, SHIP_SECTION_INNER_HULL, num_to_add, SPREAD_COMPONENTS_FROM_TOP)
          endif
        endif
      else
        // If Master Computer not available then add the usual required components for Ships and Bases
        // a. Add Bridge
        set comp_id := Sys_Get_Component_With_Name(sys_long_Player_ID, "Bridge")
        if (comp_id > 0) then
          set comp_enh_id := Sys_Get_Best_Comp_Enh_For_Component(sys_long_Player_ID, design_id, comp_id)
          call Add_Components_To_Vehicle_Design(design_id, comp_id, comp_enh_id, SHIP_SECTION_INNER_HULL, 1, SPREAD_COMPONENTS_FROM_TOP)
        else
          set bool_continue_design := FALSE
        endif
      endif
      // b. Add Life Support (Multiple)
      set comp_id := Sys_Get_Component_With_Name(sys_long_Player_ID, "Life Support")
      if (comp_id > 0) then
        set comp_enh_id := Sys_Get_Best_Comp_Enh_For_Component(sys_long_Player_ID, design_id, comp_id)
        set num_to_add := Get_Number_Of_Components_To_Add(design_id, design_type, "Life Support")
        if (num_to_add > 0) then
          // Disperse Life Support components between inner and outer slots
          for index := 1 to num_to_add do
            if (vehicle_tonnage > 500) then
              if (Sys_Get_Random_Long(1, 3) < 3) then
                set slot_loc := Sys_Get_Random_Long(SHIP_SECTION_OUTER_HULL, SHIP_SECTION_INNER_HULL)
              else
                set slot_loc := SHIP_SECTION_INNER_HULL
              endif
            else
               set slot_loc := SHIP_SECTION_INNER_HULL
            endif
            // Add life support(s) to our design
            call Add_Components_To_Vehicle_Design(design_id, comp_id, comp_enh_id, slot_loc, 1, SPREAD_COMPONENTS_FROM_TOP)
          endfor
        endif
      else
        set bool_continue_design := FALSE
      endif
      // c. Add Crew Quarters (Multiple)
      set comp_id := Sys_Get_Component_With_Name(sys_long_Player_ID, "Crew Quarters")
      if (comp_id > 0) then
        set comp_enh_id := Sys_Get_Best_Comp_Enh_For_Component(sys_long_Player_ID, design_id, comp_id)
        set num_to_add := Get_Number_Of_Components_To_Add(design_id, design_type, "Crew Quarters")
        // Disperse Crew Quarter components between inner and outer slots
        if (num_to_add > 0) then
          for index := 1 to num_to_add do
            if (vehicle_tonnage > 500) then
              if (Sys_Get_Random_Long(1, 3) < 3) then
                set slot_loc := Sys_Get_Random_Long(SHIP_SECTION_OUTER_HULL, SHIP_SECTION_INNER_HULL)
              else
                set slot_loc := SHIP_SECTION_INNER_HULL
              endif
            else
               set slot_loc := SHIP_SECTION_INNER_HULL
            endif
            // Add crew quarter(s) to our design
            call Add_Components_To_Vehicle_Design(design_id, comp_id, comp_enh_id, slot_loc, 1, SPREAD_COMPONENTS_FROM_TOP)
          endfor
        endif
      else
        set bool_continue_design := FALSE
      endif
    endif

    if (bool_continue_design) then
      // d. Basic Sensors
      if Is_Component_Available("Psychic Receptors") or Is_Component_Available("Temporal Sensors") or Is_Component_Available("Tachyon Sensors") then
        if (bool_AI_Design_Add_Sensors) then
          set bool_AI_Design_Skip_Basic_Sensors := TRUE
        endif
      endif
      // Add basic sensors for most vehicles and if we don't have advanced ones on a specialty design
      if (not bool_AI_Design_Skip_Basic_Sensors) then
        set comp_id := Sys_Get_Component_With_Name(sys_long_Player_ID, "Basic Sensors")
        if (comp_id > 0) then
          set comp_enh_id := Sys_Get_Best_Comp_Enh_For_Component(sys_long_Player_ID, design_id, comp_id)
          call Add_Components_To_Vehicle_Design(design_id, comp_id, comp_enh_id, SHIP_SECTION_INNER_HULL, 1, SPREAD_COMPONENTS_FROM_TOP)
        endif
      endif
    endif
  endif

  // Add required Fighter components
  if (vehicle_type = "Fighter") then
    set comp_id := Sys_Get_Component_With_Name(sys_long_Player_ID, "Fighter Cockpit")
    if (comp_id > 0) then
      set comp_enh_id := Sys_Get_Best_Comp_Enh_For_Component(sys_long_Player_ID, design_id, comp_id)
      call Add_Components_To_Vehicle_Design(design_id, comp_id, comp_enh_id, SHIP_SECTION_INNER_HULL, 1, SPREAD_COMPONENTS_FROM_TOP)
    else
      set bool_continue_design := FALSE
    endif
  endif

  // Add required Troop Components
  if (vehicle_type = "Troop") then
    set comp_id := Sys_Get_Component_With_Name(sys_long_Player_ID, "Troop Cockpit")
    if (comp_id > 0) then
      set comp_enh_id := Sys_Get_Best_Comp_Enh_For_Component(sys_long_Player_ID, design_id, comp_id)
      call Add_Components_To_Vehicle_Design(design_id, comp_id, comp_enh_id, SHIP_SECTION_INNER_HULL, 1, SPREAD_COMPONENTS_FROM_TOP)
    else
      set bool_continue_design := FALSE
    endif
  endif

  // Add required Satellite components
  if (vehicle_type = "Satellite") then
    if Is_Component_Available("Satellite Ganglia") then
      set comp_id := Sys_Get_Component_With_Name(sys_long_Player_ID, "Satellite Ganglia")
    else
      set comp_id := Sys_Get_Component_With_Name(sys_long_Player_ID, "Satellite Computer Core")
    endif
    // Add Satellite control
    if (comp_id > 0) then
      set comp_enh_id := Sys_Get_Best_Comp_Enh_For_Component(sys_long_Player_ID, design_id, comp_id)
      call Add_Components_To_Vehicle_Design(design_id, comp_id, comp_enh_id, SHIP_SECTION_INNER_HULL, 1, SPREAD_COMPONENTS_FROM_TOP)
    else
      set bool_continue_design := FALSE
    endif
    // Add Basic Sensors for Recon satellites
    if (ai_design_type = "Satellite (Recon)") then
      set comp_id := Sys_Get_Component_With_Name(sys_long_Player_ID, "Basic Sensors")
      if (comp_id > 0) then
        set comp_enh_id := Sys_Get_Best_Comp_Enh_For_Component(sys_long_Player_ID, design_id, comp_id)
        call Add_Components_To_Vehicle_Design(design_id, comp_id, comp_enh_id, SHIP_SECTION_INNER_HULL, 1, SPREAD_COMPONENTS_FROM_TOP)
      endif
    endif
    // Add Repair Pod(s) for Repair satellites
    if (ai_design_type = "Satellite (Repair)") then
      if Is_Component_Available("Repair Pod") then
        set comp_id := Sys_Get_Component_With_Name(sys_long_Player_ID, "Repair Pod")
        set comp_enh_id := Sys_Get_Best_Comp_Enh_For_Component(sys_long_Player_ID, design_id, comp_id)
        // Small Satellites (2 pods); Large Satellites (4 pods)
        if (size_id = AI_VEHICLE_SIZE_LG_SATELLITE) then
          call Add_Components_To_Vehicle_Design(design_id, comp_id, comp_enh_id, SHIP_SECTION_OUTER_HULL, 4, SPREAD_COMPONENTS_FROM_TOP)
        else
          call Add_Components_To_Vehicle_Design(design_id, comp_id, comp_enh_id, SHIP_SECTION_OUTER_HULL, 2, SPREAD_COMPONENTS_FROM_TOP)
        endif
      endif
    endif
  endif

  // Add required Weapon Platform components
  if (vehicle_type = "Weapon Platform") then
    set comp_id := Sys_Get_Component_With_Name(sys_long_Player_ID, "Weapon Platform Control")
    if (comp_id > 0) then
      set comp_enh_id := Sys_Get_Best_Comp_Enh_For_Component(sys_long_Player_ID, design_id, comp_id)
      call Add_Components_To_Vehicle_Design(design_id, comp_id, comp_enh_id, SHIP_SECTION_INNER_HULL, 1, SPREAD_COMPONENTS_FROM_TOP)
    else
      set bool_continue_design := FALSE
    endif
    // Add Basic Sensors for Weapon Platforms
    set comp_id := Sys_Get_Component_With_Name(sys_long_Player_ID, "Basic Sensors")
    if (comp_id > 0) then
      set comp_enh_id := Sys_Get_Best_Comp_Enh_For_Component(sys_long_Player_ID, design_id, comp_id)
      call Add_Components_To_Vehicle_Design(design_id, comp_id, comp_enh_id, SHIP_SECTION_INNER_HULL, 1, SPREAD_COMPONENTS_FROM_TOP)
    endif
  endif

  // Add required Drone components
  if (vehicle_type = "Drone") then
    // Drone Ganglia (Organic)
    if Is_Component_Available("Drone Ganglia") then
      set comp_id := Sys_Get_Component_With_Name(sys_long_Player_ID, "Drone Ganglia")
    else
      // Psychic Drone Interface
      if Is_Component_Available("Psychic Drone Interface") then
        set comp_id := Sys_Get_Component_With_Name(sys_long_Player_ID, "Psychic Drone Interface")
      else
        // Kamikaze or non-combat Drones
        if (bool_AI_Design_Add_No_Weapons) then
          set comp_id := Sys_Get_Component_With_Name(sys_long_Player_ID, "Drone Computer Core")
        else
          // Drones with regular weapons need to use a Combat Drone Computer Core
          set comp_id := Sys_Get_Component_With_Name(sys_long_Player_ID, "Combat Drone Computer Core")
        endif
      endif
    endif
    // Add Drone control to design
    if (comp_id > 0) then
      set comp_enh_id := Sys_Get_Best_Comp_Enh_For_Component(sys_long_Player_ID, design_id, comp_id)
      call Add_Components_To_Vehicle_Design(design_id, comp_id, comp_enh_id, SHIP_SECTION_INNER_HULL, 1, SPREAD_COMPONENTS_FROM_TOP)
    else
      set bool_continue_design := FALSE
    endif
    // Add Repair Pod(s) for Repair Drones
    if (ai_design_type = "Drone (Repair)") then
      if Is_Component_Available("Repair Pod") then
        set comp_id := Sys_Get_Component_With_Name(sys_long_Player_ID, "Repair Pod")
        set comp_enh_id := Sys_Get_Best_Comp_Enh_For_Component(sys_long_Player_ID, design_id, comp_id)
        // Small Drones (2 pods); Large Drones (4 pods)
        if (size_id = AI_VEHICLE_SIZE_LG_DRONE) then
          call Add_Components_To_Vehicle_Design(design_id, comp_id, comp_enh_id, SHIP_SECTION_OUTER_HULL, 4, SPREAD_COMPONENTS_FROM_TOP)
        else
          call Add_Components_To_Vehicle_Design(design_id, comp_id, comp_enh_id, SHIP_SECTION_OUTER_HULL, 2, SPREAD_COMPONENTS_FROM_TOP)
        endif
      endif
    endif
  endif

  //  11. Add Engines to desired amount (Max engines)
  if (Sys_Is_Vehicle_Size_Ship(size_id)) then
    //  e. Add Engines
    set comp_id := Get_Best_Component_Of_Type("Engine")
    if (comp_id > 0) then
      set comp_enh_id := Sys_Get_Best_Comp_Enh_For_Component(sys_long_Player_ID, design_id, comp_id)
      set num_to_add := Get_Number_Of_Components_To_Add(design_id, design_type, "Engine")
      call Add_Components_To_Vehicle_Design(design_id, comp_id, comp_enh_id, SHIP_SECTION_OUTER_HULL, num_to_add, SPREAD_COMPONENTS_FROM_BOTTOM)
    endif
  else
    if (vehicle_type = "Fighter") then
      set comp_id := Get_Best_Component_Of_Type("Small Engine")
      if (comp_id > 0) then
        set comp_enh_id := Sys_Get_Best_Comp_Enh_For_Component(sys_long_Player_ID, design_id, comp_id)
        set num_to_add := Get_Number_Of_Components_To_Add(design_id, design_type, "Small Engine")
        call Add_Components_To_Vehicle_Design(design_id, comp_id, comp_enh_id, SHIP_SECTION_OUTER_HULL, num_to_add, SPREAD_COMPONENTS_FROM_BOTTOM)
      endif
    endif
    if (vehicle_type = "Troop") then
      set comp_id := Get_Best_Component_Of_Type("Ground Movement")
      if (comp_id > 0) then
        set comp_enh_id := Sys_Get_Best_Comp_Enh_For_Component(sys_long_Player_ID, design_id, comp_id)
        set num_to_add := Get_Number_Of_Components_To_Add(design_id, design_type, "Ground Movement")
        call Add_Components_To_Vehicle_Design(design_id, comp_id, comp_enh_id, SHIP_SECTION_OUTER_HULL, num_to_add, SPREAD_COMPONENTS_FROM_BOTTOM)
      endif
    endif
    if (vehicle_type = "Drone") then
      set comp_id := Get_Best_Component_Of_Type("Engine")
      if (comp_id > 0) then
        set comp_enh_id := Sys_Get_Best_Comp_Enh_For_Component(sys_long_Player_ID, design_id, comp_id)
        set num_to_add := Get_Number_Of_Components_To_Add(design_id, design_type, "Engine")
        call Add_Components_To_Vehicle_Design(design_id, comp_id, comp_enh_id, SHIP_SECTION_OUTER_HULL, num_to_add, SPREAD_COMPONENTS_FROM_BOTTOM)
      endif
    endif
  endif

  // f. Add Colony Modules to Colonizers
  if (ai_vehicle_size = AI_VEHICLE_SIZE_COLONY_SHIP) then
    if (ai_design_type = "Colonizer (Rock)") then
      set comp_id := Sys_Get_Component_With_Name(sys_long_Player_ID, "Rock Colony Module")
      if (comp_id > 0) then
        set comp_enh_id := Sys_Get_Best_Comp_Enh_For_Component(sys_long_Player_ID, design_id, comp_id)
        call Add_Components_To_Vehicle_Design(design_id, comp_id, comp_enh_id, SHIP_SECTION_OUTER_HULL, 1, SPREAD_COMPONENTS_FROM_TOP)
      else
        set bool_continue_design := FALSE
      endif
    endif
    if (ai_design_type = "Colonizer (Ice)") then
      set comp_id := Sys_Get_Component_With_Name(sys_long_Player_ID, "Ice Colony Module")
      if (comp_id > 0) then
        set comp_enh_id := Sys_Get_Best_Comp_Enh_For_Component(sys_long_Player_ID, design_id, comp_id)
        call Add_Components_To_Vehicle_Design(design_id, comp_id, comp_enh_id, SHIP_SECTION_OUTER_HULL, 1, SPREAD_COMPONENTS_FROM_TOP)
      else
        set bool_continue_design := FALSE
      endif
    endif
    if (ai_design_type = "Colonizer (Gas)") then
      set comp_id := Sys_Get_Component_With_Name(sys_long_Player_ID, "Gas Giant Colony Module")
      if (comp_id > 0) then
        set comp_enh_id := Sys_Get_Best_Comp_Enh_For_Component(sys_long_Player_ID, design_id, comp_id)
        call Add_Components_To_Vehicle_Design(design_id, comp_id, comp_enh_id, SHIP_SECTION_OUTER_HULL, 1, SPREAD_COMPONENTS_FROM_TOP)
      else
        set bool_continue_design := FALSE
      endif
    endif
  endif

  // g. Add Fighter or Drone Launchers and Cargo Bays to Carriers
  // Set number of "slots" available for launchers or cargo bays
  if (design_type = "Carrier (Fighter)") or (design_type = "Carrier (Drone)") then
    case vehicle_tonnage
      500:
        set num_slots := 8
        set slot_mod := 1
      550:
        set num_slots := 8
        set slot_mod := 1
      600:
        set num_slots := 10
        set slot_mod := 2
      1000:
        set num_slots := 16
        set slot_mod := 2
      1100:
        set num_slots := 18
        set slot_mod := 2
      1200:
        set num_slots := 20
        set slot_mod := 2
      1500:
        set num_slots := 24
        set slot_mod := 2
      1650:
        set num_slots := 28
        set slot_mod := 2
      1800:
        set num_slots := 32
        set slot_mod := 4
    endcase

    // Backup for custom sizes
    if (num_slots = 0) then
      set num_slots := Sys_Divide_Long(vehicle_tonnage, 60)
    endif

    // Adjust Launcher:Cargo ratio
    set num_launchers := Sys_Trunc(num_slots * lng_AI_Design_Ratio_Launchers / 100)
    set num_cargo_bays := num_slots - num_launchers

    // Carriers (Fighters)
    if (design_type = "Carrier (Fighter)") then
      set comp_name := "Fighter Launcher"
    else
      set comp_name := "Drone Launcher"
    endif
    // Add Launchers
    set comp_id := Sys_Get_Component_With_Name(sys_long_Player_ID, comp_name)
    if (comp_id > 0) then
      set comp_enh_id := Sys_Get_Best_Comp_Enh_For_Component(sys_long_Player_ID, design_id, comp_id)
      call Add_Components_To_Vehicle_Design(design_id, comp_id, comp_enh_id, SHIP_SECTION_OUTER_HULL, num_launchers, SPREAD_COMPONENTS_FROM_TOP)
    endif
    // Cargo Bays
    set comp_id := Sys_Get_Component_With_Name(sys_long_Player_ID, "Cargo Bay")
    if (comp_id > 0) then
      set comp_enh_id := Sys_Get_Best_Comp_Enh_For_Component(sys_long_Player_ID, design_id, comp_id)
      call Add_Components_To_Vehicle_Design(design_id, comp_id, comp_enh_id, SHIP_SECTION_OUTER_HULL, num_cargo_bays, SPREAD_COMPONENTS_FROM_TOP)
    endif
  endif

  // Add components for specialized designs
  if (bool_continue_design) then
    set comp_name := ""
    set pct_space := 0
    set comp_id := 0
    set comp_abil := ""
    set add_only_one := FALSE

    case (ai_design_type)
      "Base (Repair)":
        set comp_name := "Repair Bay"
        set pct_space := 80
      "Base (Remote Farming)":
        set comp_name := "Robo-Farmers"
        set pct_space := 80
      "Base (Remote Mining)":
        set comp_name := "Robo-Miners"
        set pct_space := 80
      "Base (Remote Refining)":
        set comp_name := "Robo-Extractors"
        set pct_space := 80
      "Base (Space Yard)":
        set comp_abil := "Space Yard"
        set add_only_one := TRUE
      "Drone (Bombardment)":
        set comp_id := Get_Wanted_Component_From_List(lst_Bombardment_Weapon_Name)
        set pct_space := 40
      "Drone (Kamikaze)":
        set comp_name := "Cobalt Warhead"
        set pct_space := 40
      "Drone (Repair)":
        set comp_name := "Repair Pod"
        set pct_space := 60
      "Satellite (Repair)":
        set comp_name := "Repair Pod"
        set pct_space := 60
      "Ship (Kamikaze)":
        set comp_name := "Cobalt Warhead"
        set pct_space := 30
      "Fighter (Kamikaze)":
        set comp_name := "Kamikaze Warhead"
        set pct_space := 40
      "Mine":
        set comp_name := "Mine Warhead"
        set pct_space := 100
      "Ship (Black Hole Creator)":
        set comp_abil := "Black Hole - Create"
        set add_only_one := TRUE
      "Ship (Black Hole Destroyer)":
        set comp_abil := "Black Hole - Destroy"
        set add_only_one := TRUE
      "Ship (Boarding)":
        set comp_name := "Boarding Parties"
        set pct_space := 20
      "Ship (Bombardment)":
        set comp_id := Get_Wanted_Component_From_List(lst_Bombardment_Weapon_Name)
        set pct_space := 20
      "Ship (Mine Layer)":
        set comp_name := "Mine Layer"
        set pct_space := 20
      "Ship (Mine Sweeper)":
        set comp_name := "Mine Sweeper"
        set pct_space := 50
      "Ship (Nebulae Creator)":
        set comp_abil := "Nebulae - Create"
        set add_only_one := TRUE
      "Ship (Nebulae Destroyer)":
        set comp_abil := "Nebulae - Destroy"
        set add_only_one := TRUE
      "Ship (Plague)":
        set comp_name := "Plague Bomb"
        set pct_space := 20
      "Ship (Planet Creator)":
        set comp_abil := "Planet Create Size"
        set add_only_one := TRUE
      "Ship (Planet Destroyer)":
        set comp_abil := "Planet Destroy Size"
        set add_only_one := TRUE
      "Ship (Remote Mining)":
        set comp_name := "Robo-Miners"
        set pct_space := 70
      "Ship (Remote Farming)":
        set comp_name := "Robo-Farmers"
        set pct_space := 70
      "Ship (Remote Refining)":
        set comp_name := "Robo-Extractors"
        set pct_space := 70
      "Ship (Repair)":
        set comp_name := "Repair Bay"
        set pct_space := 50
      "Ship (Satellite Launcher)":
        set comp_name := "Satellite Launcher"
        set pct_space := 20
      "Ship (Space Yard)":
        set comp_abil := "Space Yard"
        set add_only_one := TRUE
      "Ship (Star Creator)":
        set comp_abil := "Star - Create"
        set add_only_one := TRUE
      "Ship (Star Destroyer)":
        set comp_abil := "Star - Destroy"
        set add_only_one := TRUE
      "Ship (Storm Creator)":
        set comp_abil := "Storm - Create"
        set add_only_one := TRUE
      "Ship (Storm Destroyer)":
        set comp_abil := "Storm - Destroy"
        set add_only_one := TRUE
      "Ship (Warp Point Closer)":
        set comp_abil := "Warp Point - Close"
        set add_only_one := TRUE
      "Ship (Warp Point Opener)":
        set comp_abil := "Warp Point - Open Distance"
        set add_only_one := TRUE
    endcase

    // If adding one component...
    if (add_only_one) then
      set bool_continue_design := FALSE
      if (comp_name <> "") then
        set comp_id := Sys_Get_Component_With_Name(sys_long_Player_ID, comp_name)
      else
        set comp_id := Sys_Get_Best_Component_With_Ability(sys_long_Player_ID, comp_abil)
      endif
      if (comp_id > 0) then
        call Add_Components_To_Vehicle_Design(design_id, comp_id, 0, SHIP_SECTION_OUTER_HULL, 1, SPREAD_COMPONENTS_RANDOMLY)
        set bool_continue_design := TRUE
      endif
    else
      // How many to add if multiple?
      if (pct_space > 0) then
        set bool_continue_design := FALSE
        if (comp_name <> "") then
          set comp_id := Sys_Get_Component_With_Name(sys_long_Player_ID, comp_name)
        endif
        if (comp_id > 0) then
          set comp_enh_id := Sys_Get_Best_Comp_Enh_For_Component(sys_long_Player_ID, design_id, comp_id)
          set comp_tonnage := Sys_Get_Component_Tonnage_Space(sys_long_Player_ID, comp_id, comp_enh_id)
          set num_to_add := Sys_Trunc(((vehicle_tonnage * (pct_space / 100)) / comp_tonnage) + 0.99)
          call Add_Components_To_Vehicle_Design(design_id, comp_id, comp_enh_id, SHIP_SECTION_OUTER_HULL, num_to_add, SPREAD_COMPONENTS_FROM_TOP)
          set bool_continue_design := TRUE
        endif
      endif
    endif
  endif

  return bool_continue_design
end

//------------------------------------------------------------------------
// Mark_Old_Designs_Of_Type_Obsolete
//------------------------------------------------------------------------
function Mark_Old_Designs_Of_Type_Obsolete returns boolean
params
  design_id:                 long
  ai_design_type:            string
vars
  design_count:              long
  index:                     long
  this_ai_design_type:       string
  this_design_id:            long
  design_obsolete:           boolean := FALSE
begin

  // Is our design obsolete?
  set design_count := Sys_Get_Player_Designs_Count(sys_long_Player_ID)

  if (design_count > 0) then
    for index := 1 to design_count do
      set this_design_id := Sys_Get_Player_Design_ID(sys_long_Player_ID, index)
      if (this_design_id > 0) and (this_design_id <> design_id) then
        set this_ai_design_type := Sys_Get_Vehicle_Design_AI_Design_Type(this_design_id)
        if (this_ai_design_type = ai_design_type) then
          set design_obsolete := Sys_Mark_Vehicle_Design_Obsolete(this_design_id)
        endif
      endif
    endfor
  endif

  return design_obsolete
end

//------------------------------------------------------------------------
// Get_AI_Vehicle_Size
//------------------------------------------------------------------------
function Get_AI_Vehicle_Size returns long
params
  size_name:                 string
vars
  ai_vehicle_size:           long := 0
  vehicle_size:              long
  num_items:                 long
  index:                     long := 0
begin

  // Match the size name with the AI size constant
  set index := lst_AI_Vehicle_Sizes_Name.indexof(size_name)
  set ai_vehicle_size := lst_AI_Vehicle_Sizes_AI_Size.get(index)

  return ai_vehicle_size
end

//------------------------------------------------------------------------
// Set_Design_Type_Strategy
//------------------------------------------------------------------------
function Set_Design_Type_Strategy returns boolean
params
  design_id:                  long
  design_type:                string
vars
begin

  // Convert our ship's specific design type to its design type
  set design_type := Get_General_AI_Design_Type(design_type)

  // Assign a strategy based on our design's type
  case design_type
    "Ship (Attack)":
      call Sys_Set_Vehicle_Design_Strategy(sys_long_Player_ID, design_id, str_Atk_Ship_Atk_Strat)
    "Ship (Attack Base)":
      call Sys_Set_Vehicle_Design_Strategy(sys_long_Player_ID, design_id, str_Atk_Ship_Atk_Strat)
    "Ship (Defense)":
      call Sys_Set_Vehicle_Design_Strategy(sys_long_Player_ID, design_id, str_Def_Ship_Atk_Strat)
    "Ship (Seeker)":
      call Sys_Set_Vehicle_Design_Strategy(sys_long_Player_ID, design_id, str_Skr_Ship_Atk_Strat)
    "Ship (Bombardment)":
      call Sys_Set_Vehicle_Design_Strategy(sys_long_Player_ID, design_id, "Bombard Enemy Planet")
    "Ship (Plague)":
      call Sys_Set_Vehicle_Design_Strategy(sys_long_Player_ID, design_id, "Bombard Enemy Planet")
    "Ship (Explorer)":
      call Sys_Set_Vehicle_Design_Strategy(sys_long_Player_ID, design_id, "Maximum Range")
    "Ship (Patrol)":
      call Sys_Set_Vehicle_Design_Strategy(sys_long_Player_ID, design_id, "Short Range")
    "Ship (Scout)":
      call Sys_Set_Vehicle_Design_Strategy(sys_long_Player_ID, design_id, "Maximum Range")
    "Ship (Kamikaze)":
      call Sys_Set_Vehicle_Design_Strategy(sys_long_Player_ID, design_id, "Kamikaze")
    "Ship (Point-Defense)":
      call Sys_Set_Vehicle_Design_Strategy(sys_long_Player_ID, design_id, "Short Range")
    "Ship (Repair)":
      call Sys_Set_Vehicle_Design_Strategy(sys_long_Player_ID, design_id, "Don't Get Hurt")
    "Ship (Space Yard)":
      call Sys_Set_Vehicle_Design_Strategy(sys_long_Player_ID, design_id, "Don't Get Hurt")
    "Transport (Supply)":
      call Sys_Set_Vehicle_Design_Strategy(sys_long_Player_ID, design_id, "Don't Get Hurt")
    "Colonizer (Rock)":
      call Sys_Set_Vehicle_Design_Strategy(sys_long_Player_ID, design_id, "Kamikaze")
    "Colonizer (Ice)":
      call Sys_Set_Vehicle_Design_Strategy(sys_long_Player_ID, design_id, "Kamikaze")
    "Colonizer (Gas)":
      call Sys_Set_Vehicle_Design_Strategy(sys_long_Player_ID, design_id, "Kamikaze")
    "Base (Defense)":
      call Sys_Set_Vehicle_Design_Strategy(sys_long_Player_ID, design_id, "Optimal Range")
    "Base (Space Yard)":
      call Sys_Set_Vehicle_Design_Strategy(sys_long_Player_ID, design_id, "Don't Get Hurt")
    "Base (Repair)":
      call Sys_Set_Vehicle_Design_Strategy(sys_long_Player_ID, design_id, "Don't Get Hurt")
    "Transport (Population)":
      call Sys_Set_Vehicle_Design_Strategy(sys_long_Player_ID, design_id, "Don't Get Hurt")
    "Transport (Troop)":
      call Sys_Set_Vehicle_Design_Strategy(sys_long_Player_ID, design_id, "Capture Enemy Planet")
    "Transport (Cargo)":
      call Sys_Set_Vehicle_Design_Strategy(sys_long_Player_ID, design_id, "Don't Get Hurt")
    "Ship (Remote Mining)":
      call Sys_Set_Vehicle_Design_Strategy(sys_long_Player_ID, design_id, "Kamikaze")
    "Ship (Remote Farming)":
      call Sys_Set_Vehicle_Design_Strategy(sys_long_Player_ID, design_id, "Kamikaze")
    "Ship (Remote Refining)":
      call Sys_Set_Vehicle_Design_Strategy(sys_long_Player_ID, design_id, "Kamikaze")
    "Base (Remote Mining)":
      call Sys_Set_Vehicle_Design_Strategy(sys_long_Player_ID, design_id, "Don't Get Hurt")
    "Base (Remote Farming)":
      call Sys_Set_Vehicle_Design_Strategy(sys_long_Player_ID, design_id, "Don't Get Hurt")
    "Base (Remote Refining)":
      call Sys_Set_Vehicle_Design_Strategy(sys_long_Player_ID, design_id, "Don't Get Hurt")
    "Carrier (Fighter)":
      call Sys_Set_Vehicle_Design_Strategy(sys_long_Player_ID, design_id, "Maximum Range")
    "Carrier (Drone)":
      call Sys_Set_Vehicle_Design_Strategy(sys_long_Player_ID, design_id, "Maximum Range")
    "Ship (Satellite Launcher)":
      call Sys_Set_Vehicle_Design_Strategy(sys_long_Player_ID, design_id, "Don't Get Hurt")
    "Ship (Mine Layer)":
      call Sys_Set_Vehicle_Design_Strategy(sys_long_Player_ID, design_id, "Don't Get Hurt")
    "Ship (Mine Sweeper)":
      call Sys_Set_Vehicle_Design_Strategy(sys_long_Player_ID, design_id, "Don't Get Hurt")
    "Ship (Boarding Ship)":
      call Sys_Set_Vehicle_Design_Strategy(sys_long_Player_ID, design_id, "Capture Enemy Ships")
    "Fighter":
      call Sys_Set_Vehicle_Design_Strategy(sys_long_Player_ID, design_id, "Fighter Attack")
    "Fighter (Bomber)":
      call Sys_Set_Vehicle_Design_Strategy(sys_long_Player_ID, design_id, "Maximum Range")
    "Fighter (Kamikaze)":
      call Sys_Set_Vehicle_Design_Strategy(sys_long_Player_ID, design_id, "Kamikaze")
    "Mine":
      call Sys_Set_Vehicle_Design_Strategy(sys_long_Player_ID, design_id, "Kamikaze")
    "Satellite":
      call Sys_Set_Vehicle_Design_Strategy(sys_long_Player_ID, design_id, "Optimal Range")
    "Satellite (Seeker)":
      call Sys_Set_Vehicle_Design_Strategy(sys_long_Player_ID, design_id, "Maximum Range")
    "Satellite (Recon)":
      call Sys_Set_Vehicle_Design_Strategy(sys_long_Player_ID, design_id, "Don't Get Hurt")
    "Troop":
      call Sys_Set_Vehicle_Design_Strategy(sys_long_Player_ID, design_id, "Optimal Range")
    "Troop (Light)":
      call Sys_Set_Vehicle_Design_Strategy(sys_long_Player_ID, design_id, "Optimal Range")
    "Troop (Heavy)":
      call Sys_Set_Vehicle_Design_Strategy(sys_long_Player_ID, design_id, "Optimal Range")
    "Weapon Platform":
      call Sys_Set_Vehicle_Design_Strategy(sys_long_Player_ID, design_id, "Optimal Range")
    "Weapon Platform (Seeker)":
      call Sys_Set_Vehicle_Design_Strategy(sys_long_Player_ID, design_id, "Maximum Range")
    "Drone (Attack)":
      call Sys_Set_Vehicle_Design_Strategy(sys_long_Player_ID, design_id, str_Atk_Ship_Atk_Strat)
    "Drone (Defense)":
      call Sys_Set_Vehicle_Design_Strategy(sys_long_Player_ID, design_id, str_Atk_Ship_Atk_Strat)
    "Drone (Bombardment)":
      call Sys_Set_Vehicle_Design_Strategy(sys_long_Player_ID, design_id, "Kamikaze")
    "Drone (Kamikaze)":
      call Sys_Set_Vehicle_Design_Strategy(sys_long_Player_ID, design_id, "Kamikaze")
  endcase

  return TRUE
end

//------------------------------------------------------------------------